# How can I intercept AR creates/updates to automatically set

**URL:** <https://www.ruby-forum.com/t/how-can-i-intercept-ar-creates-updates-to-automatically-set/87616>\
**Category:** Rails\
**Created:** [March 4, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/how-can-i-intercept-ar-creates-updates-to-automatically-set/87616 "2007-03-04T15:43:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [March 4, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/how-can-i-intercept-ar-creates-updates-to-automatically-set/87616/1 "2007-03-04T15:43:04Z")

</div>

I would like to add functionality like the update\_on/created\_on etc  
fields and intercept updates and creates so that I can drop in data  
into certain fields at the last minute.

I am not sure what the best Railer approach would be. Observers?  
Override some AR methods from within my model? Any guidance on this  
would be appreciated, thanks.

Chad

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [March 4, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/how-can-i-intercept-ar-creates-updates-to-automatically-set/87616/2 "2007-03-04T16:12:07Z")

</div>

Hey Chad, you can use Rail’s callbacks to handle this for you. Thus,  
if you need to do some post processing of create and update actions,  
then one can do the following within your model classes:

def after\_create

# do some stuff

end

def after\_update

# do some stuff

end

For more information, please section 19.2 of AWDwRv2

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [March 4, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/how-can-i-intercept-ar-creates-updates-to-automatically-set/87616/3 "2007-03-04T16:29:09Z")

</div>

Thanks Conrad, it was staring me in the face all this time.

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [March 5, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/how-can-i-intercept-ar-creates-updates-to-automatically-set/87616/4 "2007-03-05T12:52:17Z")

</div>

if you want to catch something before your models are saved, vs.  
after, which is what it sounds like, use before\_save callback. it  
will be called before a create or update.
