It seems as though there is no way to get the value of an attribute in before_update, BEFORE the update occurs. What exactly does before_update mean then? Is before_save called before before_update? Are they different? I''ve tried querying the database like: def before_update tran = Tran.find( self.id ) oldName = tran.Name end But it never returns the old value of Name. -- 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 -~----------~----~----~----~------~----~------~--~---
Hi Blake, On 2/18/07, Blake Miller <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > It seems as though there is no way to get the value of an attribute in > before_update, BEFORE the update occurs. > > What exactly does before_update mean then? Is before_save called before > before_update? Are they different?before_save is called before a save or an update, before_update is only called before an update (e.g., #update_attributes).> I''ve tried querying the database like: > > def before_update > tran = Tran.find( self.id ) > oldName = tran.Name > end > > But it never returns the old value of Name.Hmm, seems to work for me. Do you know if the hook is being run? If so, is it finding the old record? What''s the value of tran? If not, how are you doing the update? Might a validation be failing preventing the update? Regards, George. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
George Ogata wrote:> Hi Blake, > > On 2/18/07, Blake Miller <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> It seems as though there is no way to get the value of an attribute in >> before_update, BEFORE the update occurs. >> >> What exactly does before_update mean then? Is before_save called before >> before_update? Are they different? > > before_save is called before a save or an update, before_update is > only called before an update (e.g., #update_attributes). > >> I''ve tried querying the database like: >> >> def before_update >> tran = Tran.find( self.id ) >> oldName = tran.Name >> end >> >> But it never returns the old value of Name. > > Hmm, seems to work for me. > > Do you know if the hook is being run? If so, is it finding the old > record? What''s the value of tran? > > If not, how are you doing the update? Might a validation be failing > preventing the update? > > Regards, > George.Thanks George for the hints. You got me thinking that maybe my before_update function wasn''t being called at all. But since that didn''t seem too likely (cause I''ve got the function declaration just as is in the Agile book), I knew that the problem had to be inside the function. This was my function: def before_update tran = Tran.find( self.id ) if Util.new.this_month_up_to_today?( tran.PaidDate ) old_amount = tran.Amount else old_amount = nil end old_account_id = tran.account.id end and in my after update, I was doing calculations on the old_amount variable, which is actually a function defined like: def old_amount=(amount) @old_amount = amount end The problem stemmed from the fact that I wasn''t doing: self.old_amount = tran.Amount in my before_update function. Thanks for the help! -- 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 -~----------~----~----~----~------~----~------~--~---