# Calling a method each time an ActiveRecord Object is accessd

**URL:** <https://www.ruby-forum.com/t/calling-a-method-each-time-an-activerecord-object-is-accessd/97488>\
**Category:** Rails\
**Created:** [July 7, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/calling-a-method-each-time-an-activerecord-object-is-accessd/97488 "2006-07-07T12:44:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [July 7, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/calling-a-method-each-time-an-activerecord-object-is-accessd/97488/1 "2006-07-07T12:44:52Z")

</div>

Hi,

I know that it is possible to execute code each time data is saved into  
a model by using the +validate+ method.

However, is there also a way to execute code each time the data is  
accessed, not just when it is changed?

Bye,  
Winsmith

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [July 7, 2006, 10:27pm UTC](https://www.ruby-forum.com/t/calling-a-method-each-time-an-activerecord-object-is-accessd/97488/2 "2006-07-07T22:27:17Z")

</div>

Daniel J. wrote:

> I know that it is possible to execute code each time data is saved into  
> a model by using the +validate+ method.
> 
> However, is there also a way to execute code each time the data is  
> accessed, not just when it is changed?

You can create your own accessor methods for a model’s attributes and  
put any code you want in there. The attributes hash holds the model’s  
attributes so you can get to it from the accessor.

def phone  
self.format\_phone\_number(attributes[:phone])  
end

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [July 8, 2006, 9:18pm UTC](https://www.ruby-forum.com/t/calling-a-method-each-time-an-activerecord-object-is-accessd/97488/3 "2006-07-08T21:18:23Z")

</div>

On 7/7/06, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def phone  
> self.format\_phone\_number(attributes[:phone])  
> end

There are also the ActiveRecord::Callbacks that might do what you  
need; specifically the after\_find and after\_initialize might work  
nicely. They have some weird caveats so check out the docs before  
trying them.

[http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html](http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html)

Cheers,  
Chuck V.

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [July 13, 2006, 2:11pm UTC](https://www.ruby-forum.com/t/calling-a-method-each-time-an-activerecord-object-is-accessd/97488/4 "2006-07-13T14:11:08Z")

</div>

I got it! Thanks a lot you two!

Josh, btw, thanks a lot for the excellent has\_many :through explanation  
in your blog!

bye,  
Daniel

---

<div class="post-metadata">

**Author:** ![winsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a698b9/32.png) [@winsmith](https://www.ruby-forum.com/u/winsmith)\
**Post date:** [July 13, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/calling-a-method-each-time-an-activerecord-object-is-accessd/97488/5 "2006-07-13T18:15:08Z")

</div>

Quick correction for later viewers: It appears that the attributes hash  
isn’t created with :symbols as implied above. So above example would be

def phone  
self.format\_phone\_number(attributes[“phone”])  
end
