In Ruby on Rails, if you declare accepts_nested_attributes_for
in your model, autosave
is set to true for the child association. Is this necessary?
According to my understanding, Rails will already validate all new and changed children without declaring autosave: true
. It seems that would cover all cases where you are accepting nested attributes for the child association. However, with autosave: true
the child now gets validated every time the parent is saved, even if the child is unchanged.
This can have major unintended consequences, especially if, for instance, you modify the child model in such a way that a large amount of your records are invalid.
Try setting validate: false
on the association. You can see from http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html that activating autosave
(through accepts_nested_attributes_for
in our case) always validates the record unless you use validate: false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With