How to hook into model attribute changes?

John Topley wrote:

What's the best way to hook into any changes made to the value of a single model attribute? I need to know when the encrypted_password attribute on my User model changes i.e. when the user has changed their password. I'm not interested in the actual changed value, I just need to know when it's been changed so that I can regenerate their API key as well.

Something like

class User    def encrypted_password=(ep)      self.api_key = API.gen_key(ep) # if ep != encrypted_password      super    end end

The condition isn't necessary in this case because the encrypted_password is only set if a password change is detected, not on every form submission by the user.