📖 Scenario: You are building a simple Rails model to manage books in a library. You want to automatically set some values and run checks when books are created or updated.
🎯 Goal: Learn how to use Rails callbacks to run code before and after saving a record.
📋 What You'll Learn
Create a Rails model with attributes
Add a callback to set a default value before saving
Add a callback to print a message after saving
Use the correct Rails callback methods
💡 Why This Matters
🌍 Real World
Callbacks in Rails models help automate common tasks like setting default values or logging changes without repeating code.
💼 Career
Understanding callbacks is essential for Rails developers to write clean, maintainable code that reacts to data changes automatically.
Progress0 / 4 steps
1
Create the Book model with attributes
Create a Rails model class called Book that inherits from ApplicationRecord and has attributes title and status.
Ruby on Rails
Hint
Use attr_accessor to define attributes for this example.
2
Add a before_save callback to set default status
Add a before_save callback in the Book model that sets status to 'available' if it is nil.
Ruby on Rails
Hint
Use before_save :set_default_status and define the method set_default_status.
3
Add an after_save callback to print a message
Add an after_save callback in the Book model that calls a method print_saved_message which prints 'Book saved successfully'.
Ruby on Rails
Hint
Use after_save :print_saved_message and define the method print_saved_message with a puts statement.
4
Complete the Book model with callbacks
Ensure the Book model includes attr_accessor :title, :status, the before_save :set_default_status callback, and the after_save :print_saved_message callback with their respective methods.
Ruby on Rails
Hint
Review all previous steps and ensure the model has all parts combined correctly.
Practice
(1/5)
1. What is the main purpose of callbacks in Rails models?
easy
A. To run code automatically at specific points in a model's life
B. To manually call methods from controllers
C. To style the HTML output of views
D. To handle database migrations
Solution
Step 1: Understand what callbacks do
Callbacks are hooks that run code automatically during certain model events like saving or deleting.
Step 2: Identify the correct purpose
Callbacks help automate tasks at specific points in a model's lifecycle, not manual calls or styling.
Final Answer:
To run code automatically at specific points in a model's life -> Option A
Quick Check:
Callbacks = automatic code run [OK]
Hint: Callbacks run code automatically at model events [OK]
Common Mistakes:
Confusing callbacks with controller methods
Thinking callbacks style views
Mixing callbacks with database migrations
2. Which of the following is the correct syntax to define a callback that runs before saving a record?
easy
A. on_save :method_name
B. after_save :method_name
C. before_save do method_name end
D. before_save :method_name
Solution
Step 1: Recall callback syntax
Rails uses before_save :method_name to run a method before saving a record.
Step 2: Check other options
after_save runs after saving, on_save is invalid, and the block syntax is incorrect here.