我想要以下内容:
@user.update_attributes(:name => "Obama", :profile => { :current_location => 'US' })
其中用户 has_one 个人资料。
我想要以下内容:
@user.update_attributes(:name => "Obama", :profile => { :current_location => 'US' })
其中用户 has_one 个人资料。
使它们成为“嵌套属性”。文档说:
考虑一个有一个 Avatar 的 Member 模型:
class Member < ActiveRecord::Base
has_one :avatar
accepts_nested_attributes_for :avatar
end
...
允许您通过会员更新头像:
params = { :member' => { :avatar_attributes => { :id => '2', :icon => 'sad' } } }
member.update_attributes params['member']
member.avatar.icon # => 'sad'
正如 bjelli 所说,这accepts_nested_attributes_for
是您可能想要的方法。需要注意的是,配置文件的 :id 属性的传入允许它识别它是您想要执行的更新,这一点很重要。
我建议阅读这个nested_attributes.rb评论以了解更多:)