Forum: Ruby on Rails detecting attribute changes in callbacks

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6b3d7ffaa5e30c4d2609a27cc52802fb?d=identicon&s=25 Ben Mabey (bmabey)
on 2007-05-07 04: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?
Thanks,
Ben
6b3d7ffaa5e30c4d2609a27cc52802fb?d=identicon&s=25 Ben Mabey (bmabey)
on 2007-05-07 06:18
(Received via mailing list)
After looking at some previous threads it looks like th best way to do
with
is to overwrite the attribute writer in the model like so:
  def name=(val)
    @old_name = (val == name) ? nil : name
    super val
  end

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
after
looking in the docs it appears that they are.  If I am mistaken please
let
me know.  But otherwise, sorry for talking to myself on the mailing list
:)
-Ben
This topic is locked and can not be replied to.