Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - why does `accepts_nested_attributes_for` set `autosave` to true

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.

like image 561
Richard Jones Avatar asked Sep 09 '13 16:09

Richard Jones


1 Answers

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

like image 100
Paul Odeon Avatar answered Nov 01 '22 07:11

Paul Odeon