Forum: Ruby on Rails deleting model using optimistic locking results in ActiveRecord::StaleObjectError

A61a5cac149af9d66ddbbc23d6f152da?d=identicon&s=25 Cathal O'Riordan (coriordan)
on 2013-01-10 17:30
(Received via mailing list)

I have a model defined like so:

class Course < ActiveRecord::Base
  has_many :attendances, :dependent => :destroy
  has_many :attendees, :through => :attendances, :source => :user


when I attempt to delete a Course model, it results in an
ActiveRecord::StaleObjectError. Reviewing the resulting SQL calls shows
that the 'lock_version' column gets incremented on each dependent
'Attendance' model that is deleted. When the Course model is deleted,
lock_version no longer matches the persisted row in the database, and
the StaleObjectError. Any help on how to handle this situation would be
greatly appreciated.

A61a5cac149af9d66ddbbc23d6f152da?d=identicon&s=25 Cathal O'Riordan (coriordan)
on 2013-01-11 11:29
(Received via mailing list)
The only workaround to this I've been able to find is to use :dependent
:delete_all. This performs a general delete of related objects and
update the lock_version on the parent.

Bb8674a7b454ef2630dcc5ca8f38c60b?d=identicon&s=25 unknown (Guest)
on 2013-09-19 14:23
(Received via mailing list)
Hi Coriordan,
That worked for me, many thanks, but only drawback is, delete_all wont
the callbacks in children classes.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.