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

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 Cheung wrote in post #971451:

Frederick Cheung wrote in post #971451:

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!

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]