views:

56

answers:

2

Hi, I want to something like the following:

@user.update_attributes(:name => "Obama", :profile => { :current_location => 'US' })

where User has_one profile.

+2  A: 

Make them 'nested attributes'. The documentation says:

Consider a Member model that has one Avatar:

  class Member < ActiveRecord::Base
    has_one :avatar
    accepts_nested_attributes_for :avatar
  end

...

allows you to update the avatar through the member:

  params = { :member' => { :avatar_attributes => { :id => '2', :icon => 'sad' } } }
  member.update_attributes params['member']
  member.avatar.icon # => 'sad'
bjelli
+1  A: 

As bjelli has stated it's the accepts_nested_attributes_for method that you probably want here. It's important to notice that it is the passing in of the profile's :id attribute that allows it to recognize it's an update you want to peform.

I would recommend reading this the nested_attributes.rb comments to understand more : )

tsdbrown