A callback in Rails is a method that gets called at certain points in an object's life cycle, like before saving or after creating a record.
Click to reveal answer
beginner
Name two common types of callbacks in Rails.
Two common callbacks are before_save and after_create. They run before saving and after creating a record, respectively.
Click to reveal answer
intermediate
How do callbacks help in managing data integrity?
Callbacks let you run code automatically to check or change data before saving, ensuring the data stays correct and consistent.
Click to reveal answer
intermediate
What is the difference between before_save and after_save callbacks?
before_save runs before the record is saved to the database, allowing changes or checks. after_save runs after the record is saved, useful for actions that depend on the saved data.
Click to reveal answer
intermediate
Can callbacks stop a save operation in Rails? How?
Yes, callbacks like before_save can stop saving by throwing an exception or by adding errors to the object, which prevents the save.
Click to reveal answer
Which callback runs right before a record is saved in Rails?
Abefore_create
Bbefore_save
Cafter_save
Dafter_create
✗ Incorrect
before_save runs just before saving a record, allowing changes or checks.
What happens if a before_save callback returns false?
AThe save operation is canceled
BThe save operation continues
CAn error is raised immediately
DThe callback is ignored
✗ Incorrect
Returning false in a before_save callback used to stop the save operation in older Rails versions, but in modern Rails versions, you should throw an exception or add errors to stop saving.
Which callback would you use to run code after a new record is created?
Aafter_create
Bbefore_create
Cbefore_save
Dafter_save
✗ Incorrect
after_create runs after a new record is successfully created.
Callbacks in Rails are mainly used to:
AHandle user authentication
BManually trigger database queries
CReplace model validations
DAutomatically run code at certain object lifecycle events
✗ Incorrect
Callbacks automatically run code at specific points like before saving or after creating.
Which callback is best to modify data before saving it to the database?
Aafter_create
Bafter_save
Cbefore_save
Dafter_destroy
✗ Incorrect
before_save allows you to change data before it is saved.
Explain what Rails callbacks are and give two examples of when they run.
Think about moments in a record's life like saving or creating.
You got /3 concepts.
Describe how callbacks can be used to prevent saving invalid data in Rails.
Consider how callbacks can stop an action if conditions are not met.
You got /4 concepts.
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.