Forum: Ruby on Rails How can I intercept AR creates/updates to automatically set

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5a8dfcbdf0ab9b87d1299ad228e4af2b?d=identicon&s=25 Chad Thatcher (Guest)
on 2007-03-04 15:43
(Received via mailing list)
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.

40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2007-03-04 16:12
(Received via mailing list)
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

def after_update
  # do some stuff

For more information, please section 19.2 of AWDwRv2

Good luck,

5a8dfcbdf0ab9b87d1299ad228e4af2b?d=identicon&s=25 Chad Thatcher (Guest)
on 2007-03-04 16:29
(Received via mailing list)
Thanks Conrad, it was staring me in the face all this time.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-03-05 12:52
(Received via mailing list)
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.
This topic is locked and can not be replied to.