# Detecting attribute changes in callbacks

**URL:** <https://www.ruby-forum.com/t/detecting-attribute-changes-in-callbacks/103493>\
**Category:** Rails\
**Created:** [May 7, 2007, 4:50am UTC](https://www.ruby-forum.com/t/detecting-attribute-changes-in-callbacks/103493 "2007-05-07T04:50:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [May 7, 2007, 4:50am UTC](https://www.ruby-forum.com/t/detecting-attribute-changes-in-callbacks/103493/1 "2007-05-07T04:50:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [May 7, 2007, 6:18am UTC](https://www.ruby-forum.com/t/detecting-attribute-changes-in-callbacks/103493/2 "2007-05-07T06:18:43Z")

</div>

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
