Learned a lot about nested attributes and Rails

It’s been a frustrating night trying to figure out how accepts_nested_attributes_for, autosave, dirtyness, and Rails versions all fit together.  Going to try and summarize my findings and hopefully I’ll get it right.

  • accepts_nested_attributes_for turns on autosave
  • autosave will cause any loaded associations to be saved when save for the parent object is called
  • the key word is “loaded”, so a find with an include will cause the association to be loaded, and thus autosaved (took my a long time to track this down)
  • in Rails 2.3.5 the association is saved regardless of dirty state
  • in Rails 2.3.8 the association is saved only if dirty

2 Comments

  1. Thanks so much for this: I’ve been wondering for days why my nested _deletes only work in 2.3.5 and not in 2.3.8 (eventually wrote a set of tests to prove the difference to my boss).

    The inevitable next question is: how does one go about ensuring that the objects end up appropriately marked as dirty, when the need arises.

    Back to the API…

Leave a Reply to Scatman Dan Cancel reply

Your email address will not be published. Required fields are marked *