# Using accepts\_nested\_attributes\_for with assign\_attributes means immediate saving of associated model

**URL:** <https://discuss.rubyonrails.org/t/using-accepts-nested-attributes-for-with-assign-attributes-means-immediate-saving-of-associated-model/70525>\
**Category:** rubyonrails-core\
**Created:** [April 17, 2015, 5:21am UTC](https://discuss.rubyonrails.org/t/using-accepts-nested-attributes-for-with-assign-attributes-means-immediate-saving-of-associated-model/70525 "2015-04-17T05:21:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Barnabe](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@Jason\_Barnabe](https://discuss.rubyonrails.org/u/Jason_Barnabe)\
**Post date:** [April 17, 2015, 5:21am UTC](https://discuss.rubyonrails.org/t/using-accepts-nested-attributes-for-with-assign-attributes-means-immediate-saving-of-associated-model/70525/1 "2015-04-17T05:21:49Z")

</div>

Previously raised at [https://github.com/rails/rails/issues/17368](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=](https://github.com/rails/rails/blob/master/activemodel/lib/active_model/attribute_assignment.rb#L46) 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.

---

<div class="post-metadata">

**Author:** ![Ross\_Philip](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@Ross\_Philip](https://discuss.rubyonrails.org/u/Ross_Philip)\
**Post date:** [April 13, 2017, 8:29pm UTC](https://discuss.rubyonrails.org/t/using-accepts-nested-attributes-for-with-assign-attributes-means-immediate-saving-of-associated-model/70525/2 "2017-04-13T20:29:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jox](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jox/32/1899_2.png) [@jox](https://discuss.rubyonrails.org/u/jox)\
**Post date:** [May 25, 2020, 10:31pm UTC](https://discuss.rubyonrails.org/t/using-accepts-nested-attributes-for-with-assign-attributes-means-immediate-saving-of-associated-model/70525/3 "2020-05-25T22:31:43Z")

</div>

May I bump this and stress that this is not just a nice to have or not intuitive. It’s a serious issue and even kind of dangerous. It’s been reported 5 years ago and it’s still a problem. Please review the Github thread or request further information so it can be fixed.

OR alternatively, clarify for whatever reason this might be “by design” or hint us at what we’re missing.

Thanks!

---

<div class="post-metadata">

**Author:** ![joallard](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/joallard/32/990_2.png) [@joallard](https://discuss.rubyonrails.org/u/joallard)\
**Post date:** [May 26, 2020, 1:27am UTC](https://discuss.rubyonrails.org/t/using-accepts-nested-attributes-for-with-assign-attributes-means-immediate-saving-of-associated-model/70525/4 "2020-05-26T01:27:32Z")

</div>

Interesting. We’ve been raising a lot of improvement ideas this month, but this one truly sounds to me as a “WTF”.

---

<div class="post-metadata">

**Author:** ![jox](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jox/32/1899_2.png) [@jox](https://discuss.rubyonrails.org/u/jox)\
**Post date:** [May 26, 2020, 7:28am UTC](https://discuss.rubyonrails.org/t/using-accepts-nested-attributes-for-with-assign-attributes-means-immediate-saving-of-associated-model/70525/5 "2020-05-26T07:28:59Z")

</div>

“WTF” describes it quite nicely. 🙂

---

<div class="post-metadata">

**Author:** ![petrik](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/petrik/32/1847_2.png) [@petrik](https://discuss.rubyonrails.org/u/petrik)\
**Post date:** [May 26, 2020, 2:13pm UTC](https://discuss.rubyonrails.org/t/using-accepts-nested-attributes-for-with-assign-attributes-means-immediate-saving-of-associated-model/70525/6 "2020-05-26T14:13:29Z")

</div>

Last year I worked on a PR to make make autosave optional for `accepts_nested_attributes_for`[https://github.com/rails/rails/commit/d9c56c87f27cb58ca1e03be2a24cb2ecd6c50675](https://github.com/rails/rails/commit/d9c56c87f27cb58ca1e03be2a24cb2ecd6c50675)

---

<div class="post-metadata">

**Author:** ![jaedb](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jaedb/32/4078_2.png) [@jaedb](https://discuss.rubyonrails.org/u/jaedb)\
**Post date:** [September 8, 2021, 11:59pm UTC](https://discuss.rubyonrails.org/t/using-accepts-nested-attributes-for-with-assign-attributes-means-immediate-saving-of-associated-model/70525/7 "2021-09-08T23:59:27Z")

</div>

Has there been any response for the core development team on this? As @jox points out this is a dangerous behaviour; **not** persisting association attributes when the association fails validation definitely feels like a bug fix rather than a feature request.

I am somewhat confused as to why this hasn’t received any love from core team.

@petrik did you get any traction with your PR? I couldn’t find your commit in any specific PR.

---

<div class="post-metadata">

**Author:** ![Timmitry](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/timmitry/32/14217_2.png) [@Timmitry](https://discuss.rubyonrails.org/u/Timmitry)\
**Post date:** [July 7, 2026, 12:27pm UTC](https://discuss.rubyonrails.org/t/using-accepts-nested-attributes-for-with-assign-attributes-means-immediate-saving-of-associated-model/70525/8 "2026-07-07T12:27:55Z")

</div>

11 years and this is still unsolved? I would love a solution for this, with which I can assign attributes to the parent record, set the new has\_many records in memory, and then save everything in one go (transaction!)…
