Forum: Ruby on Rails detecting attribute changes in callbacks

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ben M. (Guest)
on 2007-05-07 06:50
(Received via mailing list)
Hey all,
What is the best/easiest way to detect if a certain AR model
attrirbute has changed in a callback?      For example, I have a date
field in my model that needs to be updated when a certain attribute
has been changed.  What is the best way to do this in a call back?
Ben M. (Guest)
on 2007-05-07 08:18
(Received via mailing list)
After looking at some previous threads it looks like th best way to do
is to overwrite the attribute writer in the model like so:
  def name=(val)
    @old_name = (val == name) ? nil : name
    super val

And then just checking the @old_val in the callback...   I thought that
these accessors were not always used in all the update methods, but
looking in the docs it appears that they are.  If I am mistaken please
me know.  But otherwise, sorry for talking to myself on the mailing list
This topic is locked and can not be replied to.