Active Record Callbacks

Andrew Richards
2 min readAug 8, 2020

--

Active Record Callbacks are predefined methods in the ActiveRecord that allow you to perform actions in specific points of the object lifecycle. This allows us to avoid repetition in our controllers, keeping our code tidy.

Get it? Rails.

The Object Life Cycle

Very simply, the object life cycle in a normal Rails application is Creation > Update > Destruction. Create, Update, Destroy. Active Record callbacks allow us to perform actions at specific points in the cycle.

From the Active Record Documentation:

With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.

2 Ways to Implement

In order for a callback to be implemented, you must “register” them. This just means that near the top of your controller, you type the callback you wish to use, and the method that you want to fire right after.

Here is an example:

class CardsController < ApplicationControllerbefore_action :find_by_iddef update   puts @cardend
private
def find_by_id @card = Card.find_by_id(params[:id])endend

In this situation before the update method fires, the :find_by_id method will run, giving the update method access to the @card instance variable.

Keep in mind for this situation, any method that is not private will run the :find_by_id method. We don’t want this. So we can specify which methods will use the before_action like so:

before_action :find_by_id, only: [:show, :index, :delete, :update]

This first situation calls a function defined in the private section of the controller. But it is also possible to use a block to determine what to do instead of calling a method.

class CardsController < ApplicationControllerbefore_action do   @card = Card.find_by_id(params[:id])enddef update    puts @cardendend

If your action can be defined on a single line it may be cleaner to use this way.

Callbacks Available to Use

There are a ton of great predefined callback methods in Active Record. Here is the list. They are very self explanatory as to when they will be called.

Creation

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
  • after_commit/after_rollback

Update

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save
  • after_commit/after_rollback

Destroy

  • before_destroy
  • around_destroy
  • after_destroy
  • after_commit/after_rollback

This is just the tip of the iceberg when it comes to callbacks. I highly encourage you to take a look at the Active Record Callbacks documentation as it lays out more advanced and nuanced ways that you can use these magnificent methods.

Please follow me on twitter @thedrewprint and add me on LinkedIn! Thanks for reading.

--

--

Andrew Richards
Andrew Richards

Written by Andrew Richards

Software developer using Angular, React, NestJs.

No responses yet