Updated attributes

Hi,
Is it possible to know in a callback to know which attributes were
updated?
I want to do stuff only if one attribute has been updated.

Greg

On Dec 30, 1:40pm, Greg Ma [email protected] wrote:

Hi,
Is it possible to know in a callback to know which attributes were
updated?
I want to do stuff only if one attribute has been updated.

The changed and changes methods lists changed attributes. There are
also invididual predicate methods, eg name_changed?

Fred

Frederick C. wrote in post #971451:

On Dec 30, 1:40pm, Greg Ma [email protected] wrote:

Hi,
Is it possible to know in a callback to know which attributes were
updated?
I want to do stuff only if one attribute has been updated.

The changed and changes methods lists changed attributes. There are
also invididual predicate methods, eg name_changed?

Fred

Thanks I didn’t know that one!
But this will work in a before_* and not after_*

On Thu, Dec 30, 2010 at 7:59 AM, Greg Ma [email protected] wrote:

Fred

Thanks I didn’t know that one!

Yeah me too, that’s very cool, I actually have the perfect place to
apply it
today

Yes, this is a very handy feature of ActiveRecord in Rails!
Let me summarize here some related methods that come up to my mind:

  • changed? predicate if any attributes have been updated
  • name_changed? described above
  • name_was restores the previous value
  • name_change [old_value, new_value]
  • changed [list_of_changed_attributes]
  • changes [hash_of_changed_attributes_with_values]

    Thanks, Ivan Povalyukhin