ActiveRecord Callbacks
TRACK

ActiveRecord Callbacks

Avatar

You can get a full list of the ActiveRecord callbacks here.

Callbacks are a handy way of insuring specific behaviors on your models as well as managing the events that follow the callbacks.

For example, a before_destroy callback can be called to destroy associated objects. (This is best done on the association with :dependent => :destroy, but it makes sense here too.)

You can also stop the destroy process if you have dependent objects you can’t destroy by returning false.

I’ve used ActiveRecord Callbacks to:

Set default values
Format specific fields (like phone numbers)
Send emails when an object is updated or deleted
Create child objects from comma delimited lists in a string
Change an object’s state

Comments

Avatar