Rails Callbacks Cheat Sheet

In Rails, callbacks are hooks provided by Active Record that allow methods to run before or after a create, update, or destroy action occurs to an object. Since it can be hard to remember all of them and what they do, here is a quick reference for all current Rails 5 Active Record callbacks. An image version is provided at the bottom and a downloadable PDF can be found on my website.

Callback Glossary

Definitions of available Rails 5 callbacks based on the Ruby on Rails API dock. Listed in alphabetical order.

after_commit

Called after a new or existing object is committed to the database. An :on argument can be used to specify which action (create, update, or destroy) this callback should apply to.

after_create

Called after a new object is saved.

after_destroy

Called after an existing object is destroyed.

after_rollback

Called after a create, update, or destroy action is rolled back on a new or existing object.

after_save

Called after a new or existing object is saved.

after_update

Called after an existing object is saved.

after_validation

Called after a new or existing object’s validations occur.

around_create

Called before a new object is saved until yield is invoked within the method triggered by the callback. Calling yield causes the object to be saved and then any proceeding code in the method will execute.

around_destroy

Called before an existing object is destroyed until yield is invoked within the method triggered by the callback. Calling yield causes the object to be destroyed and then any proceeding code in the method will execute.

around_save

Called before a new or existing object is saved until yield is invoked within the method triggered by the callback. Calling yield causes the object to be saved and then any proceeding code in the method will execute.

around_update

Called before an existing object is saved until yield is invoked within the method triggered by the callback. Calling yield causes the object to be saved and then any proceeding code in the method will execute.

before_create

Called before a new object is saved.

before_destroy

Called before an existing object is destroyed.

before_save

Called before a new or existing object is saved.

before_update

Called before an existing object is saved.

before_validation

Called before a new or existing object’s validations occur.

Callback Order of Operations

Callbacks are invoked in a specific running order depending on whether an object is created, updated, or destroyed. Below are lists of the callbacks associated with each action and the order in which they are run as defined by the Rails Guides.

Creating an Object

before_validation
after_validation
before_save
around_save
before_create
around_create
after_create
after_save
after_commit/after_rollback

Updating an Object

before_validation
after_validation
before_save
around_save
before_update
around_update
after_update
after_save
after_commit/after_rollback

Destroying an Object

before_destroy
around_destroy
after_destroy
after_commit/after_rollback

Callback Code Example

Below is an example Cat model which demonstrates how to use callbacks. I’ve included around_create since the around_* callbacks are used differently from the others. But keep in mind that these callbacks are typically uncommon.

# == Schema Information
#
# Table name: cats
#
# id :integer(8) not null, primary key
# name :string(255) not null
#
class Cat < ApplicationRecord
before_validation :give_default_name
around_create :track_create
validates :name, presence: true

private
def give_default_name
self.name = 'Rosie' if name.blank?
end
def track_create
analytics_tracker = ExampleAnalyticsClass.new
analytics_tracker.send_update("Cat about to be created")
yield
analytics_tracker.send_update("Successful creation of new Cat!")
end
end
Rails Callbacks Cheat Sheet. Click here for a link to the PDF version.

Thank you to Sihui Huang for input on earlier versions of this post.

My goal is to be helpful ✦ Software engineer at Loom ✦ juliannaroen.com (she / her)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store