0
0
Ruby on Railsframework~10 mins

Callbacks overview in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Callbacks overview
Object Created
Before Validation Callback
Validation
After Validation Callback
Before Save Callback
Before Create or Update Callback
Save to Database
After Create or Update Callback
After Save Callback
After Commit Callback
Object Ready
Rails callbacks run in a specific order around object lifecycle events like validation and saving, allowing code to run before or after these steps.
Execution Sample
Ruby on Rails
class User < ApplicationRecord
  before_save :normalize_name
  after_create :send_welcome_email

  def normalize_name
    self.name = name.strip.titleize
  end

  def send_welcome_email
    EmailService.send_welcome(self)
  end
end
This code runs a method to clean the user's name before saving and sends a welcome email after creating the user.
Execution Table
StepCallback/EventActionEffect on ObjectNotes
1Object CreatedNew User instance initializedUser object with raw nameStart of lifecycle
2before_savenormalize_name calledName trimmed and capitalizedRuns before saving to DB
3ValidationValidates user dataChecks if user data is validIf invalid, save stops
4Save to DatabaseUser record savedUser data stored in DBPersisted user
5after_createsend_welcome_email calledWelcome email sentRuns only on new record creation
6after_saveCallback chain endsUser object saved and callbacks doneFinal step
7after_commitTransaction committedChanges finalizedEnsures DB transaction success
8EndLifecycle completeUser ready for useNo more callbacks
💡 All callbacks run in order around save; process ends after after_commit
Variable Tracker
VariableStartAfter before_saveAfter ValidationAfter SaveAfter after_createFinal
user.name" alice ""Alice""Alice""Alice""Alice""Alice"
user.persisted?falsefalsefalsetruetruetrue
email_sentfalsefalsefalsefalsetruetrue
Key Moments - 3 Insights
Why does the name change after before_save but before validation?
The execution_table shows before_save runs before validation, so normalize_name cleans the name before validations check it.
When is the welcome email sent in the lifecycle?
The after_create callback runs only after the user is saved for the first time, as shown in step 5 of the execution_table.
What happens if validation fails?
Validation happens after before_save; if it fails, the save and subsequent callbacks like after_create do not run, stopping the process early.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of user.name after the before_save callback?
A"Alice"
B" alice "
C"ALICE"
D"alice"
💡 Hint
Check the 'After before_save' column for user.name in variable_tracker.
At which step does the user record get saved to the database?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Save to Database' event in the execution_table.
If validation fails, which callback will NOT run?
Abefore_validation
Bbefore_save
Cafter_create
Dafter_commit
💡 Hint
Refer to the key_moments about validation failure stopping save and after_create callbacks.
Concept Snapshot
Rails callbacks run around object lifecycle events.
Common callbacks: before_save, after_create, after_commit.
before_save runs before validation and saving.
after_create runs only after new record creation.
Use callbacks to run code automatically at these points.
Full Transcript
Rails callbacks are special methods that run automatically at certain points in an object's lifecycle, like before saving or after creating a record. For example, before_save runs a method to clean data before the object is saved to the database. After_create runs a method to send a welcome email only after the object is first saved. The callbacks run in a specific order: object creation, before_save, validation, saving, after_create, after_save, and after_commit. If validation fails, the save and later callbacks do not run. This lets you add automatic behaviors tied to saving and creating objects in Rails.