0
0
Ruby on Railsframework~10 mins

Association callbacks in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Association callbacks
Trigger action on parent model
Check association callback type
before_add
Run callback method if defined
Modify associated objects or state
Continue action
When you add or remove associated objects in Rails, specific callbacks run before or after the change to let you customize behavior.
Execution Sample
Ruby on Rails
class Cart < ApplicationRecord
  has_many :items, before_add: :check_stock

  def check_stock(item)
    raise 'Out of stock' if item.stock <= 0
  end
end
This code runs a method before adding an item to a cart to check if the item is in stock.
Execution Table
StepActionCallback TriggeredCallback Method CalledEffectResult
1Add item with stock=5 to cart.itemsbefore_addcheck_stock(item)Checks item stockPasses, item added
2Add item with stock=0 to cart.itemsbefore_addcheck_stock(item)Checks item stockRaises 'Out of stock' error, item not added
3Remove item from cart.itemsbefore_removenone (not defined)No callbackItem removed normally
4Add item to cart.itemsafter_addnone (not defined)No callbackItem added normally
5Remove item from cart.itemsafter_removenone (not defined)No callbackItem removed normally
💡 Execution stops after each add/remove action completes or raises error.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
cart.items.count010010
item.stockvaries50555
Key Moments - 2 Insights
Why does adding an item with zero stock raise an error?
Because the before_add callback runs check_stock, which raises an error if stock is 0, stopping the add (see execution_table step 2).
What happens if no callback method is defined for after_add or before_remove?
No callback runs, so the add or remove proceeds normally without extra checks (see execution_table steps 3,4,5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cart.items.count after step 1?
A1
B0
C2
DUndefined
💡 Hint
Check the variable_tracker row for cart.items.count after Step 1.
At which step does the before_add callback prevent adding an item?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table where an error is raised during before_add.
If we define an after_remove callback, what would happen at step 5?
ACallback method runs before item removal
BCallback method runs after item removal
CNo callback runs
DItem removal is blocked
💡 Hint
After_remove callbacks run after removal actions, see concept_flow.
Concept Snapshot
Association callbacks in Rails run methods before or after adding/removing associated objects.
Use options like before_add, after_add, before_remove, after_remove in associations.
Callbacks let you check or modify objects during association changes.
If a before_* callback raises an error, the change is stopped.
Define callback methods in the model to customize behavior.
Full Transcript
In Rails, association callbacks let you run code when you add or remove objects from associations like has_many. For example, before_add runs before adding an object, and after_remove runs after removing one. You define methods in your model and link them with options like before_add: :method_name. When you add an item to a cart, the before_add callback can check if the item is in stock and stop the add if not. The execution table shows adding an item with stock 5 passes, but with stock 0 raises an error. Removing items without callbacks just works normally. This helps keep your data consistent and lets you run custom logic during association changes.