MrBanabas-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Aug-24 09:46 UTC
activerecord: detect if an association attribute has changed
Hi, just a quick question, cause so far I was nt able to find a nice and clean solution. Let s assume an apple belongs_to a tree. Now I would like to be able to detect in the apple model that the tree has changed. I would like to be able to detect changes if either apple.tree_id or apple.tree is changed. Any ideas? -- 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 -~----------~----~----~----~------~----~------~--~---
John Miller
2007-Aug-24 20:52 UTC
Re: activerecord: detect if an association attribute has cha
MrBanabas-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> Hi, > > just a quick question, cause so far I was nt able to find a nice and > clean solution. > > Let s assume an apple belongs_to a tree. > Now I would like to be able to detect in the apple model that the tree > has changed. > I would like to be able to detect changes if either apple.tree_id or > apple.tree is changed. > > Any ideas? > > -- > VolkerThis is not easy, but here is a hackish solution: Turn on optimistic locking on the Tree model. (see http://scriptedzen.blogspot.com/2007/08/optimistic-locking-in-rails-with-active.html ) This will among other things give you a new integer field that will change every time the record is saved. In the Apple model store the value of lock_version field. You can then check if this value has changed and take appropriate action. If I were doing this I would check when an Apple was loaded and then again when it''s saved. This might also work with a magic timestamp field. There is (to my knowledge) no way of knowing in real time when the Tree changes except by having the Tree itself call all the Apples that belong_to it when it saves. (Very time consuming) John Miller -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---