This project is archived and is in readonly mode.
Updating nested attributes with has_one relationship doesn't work correctly
-
Eloy Duran
- State changed from new to invalid
You should include the id of the Profile to indicate it's an update you're trying to perform.
u.attributes = {:id => 2, :profile_attributes => {:city => 'nowhere'}}For more info take a look at what the form helpers produce.
-
flip
ok, i guess you mean:
u.attributes = {:profile_attributes => {:id => 2, :city => 'nowhere'}}is this documented somewhere? Using the "old way" my approach would work also without the id:
u.profile.attributes = {:city => 'nowhere'} -
Eloy Duran
Oops, yeah that's what I meant :)
It's shown here: http://github.com/rails/rails/blob/master/activerecord/lib/active_r...
It is needed to differentiate between a new record or an existing one.
-
Greg
I'm not so sure that this should be invalid. It makes sense to need the id for has_many relationships, but for belongs_to and has_one, you should assume that they want to update the record.
In the example that flip gave, calling u.save will return no problem, and you'll end up with an orphaned profile in the database. This scenario can easily come up if you are allowing updates via REST/XML...a user can PUT
nowhereto http://<>/users/2 or wherever. The user doesn't know the ID of the nested model, but they should expect that only the city is updated in the profile, a new profile should not be created.
Unfortunately, I don't have a patch for this right now, but I believe this should be re-opened as a bug. Thanks!
-
Sébastien Luquet
Hi,
Documentation is about update_attributes
params = { :member' => { :avatar_attributes => { :id => '2', :icon => 'sad' } } }
member.update_attributes params['member']What about object creation
params = { :member' => { :avatar_attributes => { :id => '2', :icon => 'sad' } } }
Member.create params['member']Does it create new Member, update Avatar with id 2 and associate the two record together ?
-
Dmitry Polushkin
I agree that it should be reopened. Not smart to pass id for one to one.
-
Eloy Duran
I'd rather see a patch that adds something like
:only_update => true' to accepts_nested_attributes_for. Or otherwise fix it in your controller if you don't trust the client. -
Dmitry Polushkin
Question, how to solve:
class Team has_one :captain, :class_name => 'Player' accepts_nested_attributes_for :captain end class Player belongs_to :team validate :validate_team private def validate_team raise 'Should have team' unless team end endTeam.create!(:captain_attributes => {:name => 'Name Surname'})Will be risen 'Should have team' because while attributes assigns to the nested model object, belongs_to associations isn't assigns.
If you know how to solve that, please tell me.
Thank you.
-
Eloy Duran
- State changed from invalid to resolved
Today a patch for :update_only and the ability to use :inverse_of, which fixes the last issue, have both been pushed. HTH
-
Jeff Kreeftmeijer
Automatic cleanup of spam.