Flowdock

Notes posted by moiristo

RSS feed
June 29, 2011
0 thanks

Updating 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
June 20, 2011
0 thanks

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
June 17, 2011
0 thanks

select_values returns Strings for postgreSQL

Will return strings too when using postgreSQL and gem pg (0.11.0).

January 21, 2010
3 thanks

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}") }