# How to hook into model attribute changes?

**URL:** <https://www.ruby-forum.com/t/how-to-hook-into-model-attribute-changes/117922>\
**Category:** Rails\
**Created:** [October 22, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/how-to-hook-into-model-attribute-changes/117922 "2007-10-22T13:28:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [October 22, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/how-to-hook-into-model-attribute-changes/117922/1 "2007-10-22T13:28:54Z")

</div>

Hi,

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.

Thanks in advance.

John

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [October 22, 2007, 3:00pm UTC](https://www.ruby-forum.com/t/how-to-hook-into-model-attribute-changes/117922/2 "2007-10-22T15:00:13Z")

</div>

Hi John

On Oct 22, 7:28 pm, John T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> 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.

cch: You should look into callbacks. For example, I use

def after\_update(record)  
log(record, “UPDATE”)  
end

to record whether a change has been made

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [October 22, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/how-to-hook-into-model-attribute-changes/117922/3 "2007-10-22T15:46:08Z")

</div>

CCH wrote:

> Hi John
> 
> On Oct 22, 7:28 pm, John T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Hi,
> > 
> > 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.
> 
> cch: You should look into callbacks. For example, I use
> 
> def after\_update(record)  
> log(record, “UPDATE”)  
> end
> 
> to record whether a change has been made

Thanks. I did try that but the problem is that the user can change other  
settings on the screen that lets them change their password and the API  
key musn’t be regenerated if those other settings are changed. Hence, I  
need to detect a changed password only.

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [October 22, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/how-to-hook-into-model-attribute-changes/117922/4 "2007-10-22T17:08:30Z")

</div>

You could try using this plugin and a callback (after\_update maybe?)  
to determine when to regenerate the API key:

[http://svn.viney.net.nz/things/rails/plugins/acts\_as\_modified/README](http://svn.viney.net.nz/things/rails/plugins/acts_as_modified/README)

On Oct 22, 8:46 am, John T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [October 22, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/how-to-hook-into-model-attribute-changes/117922/5 "2007-10-22T17:31:14Z")

</div>

Thanks Andrew, I’ll try that out.

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [October 22, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/how-to-hook-into-model-attribute-changes/117922/6 "2007-10-22T17:43:40Z")

</div>

John T. 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.

–  
We develop, watch us RoR, in numbers too big to ignore.
