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:
- mark_for_destruction all existing children not present in the passed hash.
- Update (but not persist) all existing children present in the passed hash.
- 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.