jnunemaker/mongomapper — pull request #529 by cheald (2013-07-05 06:35:19+00:00)
Title: Don't commit associations on assign; defer them until save.

This breaks existing functionality, but is probably correct. Since it's not backwards compatible, I wanted @jnunemaker to look it over.

The general problem here is described in #218, and is that assigning associations immediately saves the parent document and the association. This is both surprising and inconsistent with how it's done in other ORMs, and I'd argue that it needs to change.

This fix defers association saves/nullifies until the owner record is saved (but after it is validated!) - this means the deprecation of `:dependent => :delete`. The current implementation makes no sense at all, IMO, since :delete infers "I don't want this model to do its normal teardown". Additionally, this means that assigning an association to nil will not immediately delete the associated record, which is extremely dangerous behavior, IMO - a simple assignment should never destroy data in the database!

The basic fix here is to just defer association commits to `before_save` and eliminate `delete` operating on associated records. Travis is happy with it, but I *did* remove the specs pertaining to the eliminated behavior to get it there :)

Anyhow, please review and let me know what you think. If the current behavior is there for a reason that I'm not comprehending, I'd love to find a way to improve it.