coriordan
2013-Jan-10 16:29 UTC
deleting model using optimistic locking results in ActiveRecord::StaleObjectError
Hi, 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, its lock_version no longer matches the persisted row in the database, and hence the StaleObjectError. Any help on how to handle this situation would be greatly appreciated. regards, Cathal. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/z94yktQoBE0J. For more options, visit https://groups.google.com/groups/opt_out.
coriordan
2013-Jan-11 10:27 UTC
Re: deleting model using optimistic locking results in ActiveRecord::StaleObjectError
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 doesn''t update the lock_version on the parent. Cathal. On Thursday, 10 January 2013 16:29:22 UTC, coriordan wrote:> > Hi, > > 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, its > lock_version no longer matches the persisted row in the database, and hence > the StaleObjectError. Any help on how to handle this situation would be > greatly appreciated. > > regards, > Cathal. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/aAt9PnTpJ6gJ. For more options, visit https://groups.google.com/groups/opt_out.
arun.kumar-q6fn+ct/wbOtG0bUXCXiUA@public.gmane.org
2013-Sep-18 13:00 UTC
Re: deleting model using optimistic locking results in ActiveRecord::StaleObjectError
Hi Coriordan, That worked for me, many thanks, but only drawback is, delete_all wont call the callbacks in children classes. On Friday, January 11, 2013 3:57:39 PM UTC+5:30, coriordan wrote:> > 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 doesn''t > update the lock_version on the parent. > > Cathal. > > On Thursday, 10 January 2013 16:29:22 UTC, coriordan wrote: >> >> Hi, >> >> 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, its >> lock_version no longer matches the persisted row in the database, and hence >> the StaleObjectError. Any help on how to handle this situation would be >> greatly appreciated. >> >> regards, >> Cathal. >> >>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c278adda-aa49-4db0-8acc-b6c91626a13b%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.