rails/rails — pull request #9448 by bdmac (2013-02-26 19:35:33+00:00)
Title: Optimistic locking destroy bug

This PR is only meant as a discussion point at this time. I do not
currently have a fix here.

There seems to be a problem when using optimistic locking with
:dependent => :destroy and :touch => true. This pull request
attempts to expose the error although I do not yet have a
fix. Please see the new test in locking_test and note that
it fails. This failure means that when attempting to destroy
a model object with :dependent => :destroy on a realtionship
where the inverse has :touch => true results in:

ActiveRecord::StaleObjectError: Attempted to destroy a stale object

If in the new test we attempted to do `p.destroy` we would trigger the
StaleObjectError. This is because the lock_version of the Person in
the database gets updated when the :reference is destroyed due to
:touch => true but the in-memory lock_version on the person object,
p, is NOT updated so when the destroy is attempted it thinks it is
stale.

Any advice on how to fix this in AR without breaking anything else
would be appreciated.