Using accepts_nested_attributes_for with assign_attributes means immediate saving of associated model

675 views
Skip to first unread message

Jason Barnabe

unread,
Apr 17, 2015, 3:00:22 AM4/17/15
to rubyonra...@googlegroups.com
Previously raised at https://github.com/rails/rails/issues/17368

When a parent model accepts_nested_attributes_for a child, and you use assign_attributes on an existing parent, the children are immediately persisted; assign_attributes is just calling children= on the parent. This results in the changes being half-saved and messes things up if you wanted to do further processing before saving.

I propose that for has_one, has_many, and has_and_belongs_to_many associations, assign_attribute avoid persisting by instead:

1. mark_for_destruction all existing children not present in the passed hash.
2. Update (but not persist) all existing children present in the passed hash.
3. Call children.build to create the new children.

I imagine this logic would go in _assign_attribute and would make use of reflect_on_association to figure out what's what. I'd be willing to attempt to implement this, if it's a change that would be desired.

Ross Philip

unread,
Apr 13, 2017, 4:31:20 PM4/13/17
to Ruby on Rails: Core
I think this would be a really good change and hope you decide to pursue it.  The current behavior is not intuitive; one would expect that assign_attributes(nested_params) not persist any changes; if I wanted to persist changes I would use update(nested_params).  I've been bitten by this and only after poring through the documentation realized why.
Reply all
Reply to author
Forward
0 new messages