# Comparing field's old value to new value

**URL:** <https://www.ruby-forum.com/t/comparing-fields-old-value-to-new-value/72670>\
**Category:** Rails\
**Created:** [November 27, 2005, 11:39am UTC](https://www.ruby-forum.com/t/comparing-fields-old-value-to-new-value/72670 "2005-11-27T11:39:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 27, 2005, 11:39am UTC](https://www.ruby-forum.com/t/comparing-fields-old-value-to-new-value/72670/1 "2005-11-27T11:39:45Z")

</div>

I want to expire caches if a certain field of an item changes. How can I  
compare the item field’s  
old value to its new value (in the create, or after\_save, etc. method)?

thanks  
csn

* * *

Yahoo! Mail - PC Magazine Editors’ Choice 2005

> **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
>
> It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 28, 2005, 4:42pm UTC](https://www.ruby-forum.com/t/comparing-fields-old-value-to-new-value/72670/2 "2005-11-28T16:42:10Z")

</div>

Hi !

There’s after\_find and after\_initialize you can use to copy the  
values. If you have only one or two fields, just override the field’s  
setter methods:

class X \< AR:Base  
def attribute=(new\_value)  
old\_value ||= read\_attribute(:attribute)  
write\_attribute(:attribute, new\_value)  
end  
end

Untested, and I’m not even sure write|read\_attribute are the right  
method names. You get the gist, though.

Bye !  
FranÃ§ois

2005/11/27, CSN [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
