MrBanabas-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2008-Feb-08 23:16 UTC
update_Attributes stale Object error
Hi, IN one of my unit tests I get an ActiveRecord::StaleObjectError: Attempted to update a stale object object.save! in object I have apart from other stuff an after_save method, which calls object.assosication.update_attributes(...) If I remove that line from my code my tests succeeds. Unfortunetly, I do not understand why an update_attributes call can generate an StaleObjectError. Maybe anybody can explain this to me? Thanks a lot in advance. Volker --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8 Feb 2008, at 23:16, MrBanabas-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> > Hi, > > IN one of my unit tests I get an ActiveRecord::StaleObjectError: > Attempted to update a stale object > > object.save! > > in object I have apart from other stuff an after_save method, which > calls > object.assosication.update_attributes(...) > > If I remove that line from my code my tests succeeds. > > Unfortunetly, I do not understand why an update_attributes call can > generate an StaleObjectError. > > Maybe anybody can explain this to me?You''re using optimistic locking. When using this (ie if you have a lock_version column), if you try to save an object when a newer version exists in the database (ie you''re trying to clobber new data with an older version) you''ll get that exception. Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
MrBanabas-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2008-Feb-09 00:13 UTC
Re: update_Attributes stale Object error
Amazing.... if I do the following: @parent = Parent.find(1) child1 = Child.new(:parent => @parent); child1.save! in after_save of child1: self.parent.update_attributes(:test => ''Hallo''); afterwards: assert_not_nil(child1.parent.test); assert_not_nil(@parent.test); The second assert will fail... I ve thought all references will go to the same object.... but it seems that was kind of wrong... Quite ugly... -- Volker --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Reasonably Related Threads
- Optimistic locking and update_attributes: not working?
- Stale object errors
- [PATCH server] Fixed db-omatic so it doesn't die due to an unhandled ActiveRecord::StaleObjectError exception.
- When to use optimistic locking?
- ActiveRecord: Attribute Setter for belongs_to relation attribute