rails/rails — pull request #10681 by jholton (2013-05-18 22:22:28+00:00)
Title: autosave_association issue that occurs when table has unique index (3.2.x backport)

This is a backport of #10417

The issue:
Per the comments in pull #3329,
If you have a table with a unique field index, and you mark a record for destruction, and you build a new record with the same value as the unique field, then when you call save, a database level unique index error will be thrown.

This happens because the record destruction happens after record creation.

In this pull request, I moved the record destruction ahead of the record creation.