Notes posted by moiristo
RSS feedUpdating nested attributes of one-to-one associations
As the documentation implicitly mentions, when updating nested attributes of a one-to-one relationship, you need to pass the ID of the nested attribute itself, otherwise a new record will be created.
This works fine:
params = { :member => { :avatar_attributes => { :id => '2', :icon => 'sad' } } }
However, the following line will build and save a new record, which is usually not what you want for one-to-one:
params = { :member => { :avatar_attributes => { :icon => 'sad' } } }
Alternatively, you can use the ‘update_only’ option. This option will ensure that an existing record will always be updated, even if the ID is not specified:
accepts_nested_attributes_for :avatar, :update_only => true
Typecasting return values
A better way to typecast the result array is to use AR’s typecasting capabilities. Example:
column = Company.columns_hash['id'] select_values("SELECT id FROM companies LIMIT 3").map do |value| column.type_cast(value) end
select_values returns Strings for postgreSQL
Will return strings too when using postgreSQL and gem pg (0.11.0).
W3CDTF Format
Here is the formatted string for the W3CDTF datetime format (http://www.w3.org/TR/NOTE-datetime). It has a semicolon in the timezone part, therefore you cannot use ‘%z’:
Time::DATE_FORMATS[:w3cdtf] = lambda { |time| time.strftime("%Y-%m-%dT%H:%M:%S#{time.formatted_offset}") }