0
0
Ruby on Railsframework~10 mins

Dependent destroy and nullify in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dependent destroy and nullify
Parent object exists
Delete parent object
Dependent destroy
Delete child objects
Child objects removed
When a parent record is deleted, dependent destroy deletes child records, while dependent nullify keeps children but removes their link to the parent.
Execution Sample
Ruby on Rails
class Author < ApplicationRecord
  has_many :books, dependent: :destroy
end

Author.find(1).destroy
Deletes an author and all their books because of dependent: :destroy.
Execution Table
StepActionParent Exists?Child Exists?Child Foreign KeyResult
1Author with id=1 existsYesYes (3 books)author_id = 1Parent and children present
2Call Author.find(1).destroyYesYes (3 books)author_id = 1Start deletion
3dependent: :destroy triggersYesYes (3 books)author_id = 1Prepare to delete children
4Delete each bookYesNonullAll 3 books deleted
5Delete authorNoNonullAuthor deleted, children gone
6EndNoNonullDeletion complete
💡 Author and all dependent books are deleted, no children remain.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5
Author.exists?(1)truetruetruefalse
Books.count with author_id=13300
Key Moments - 2 Insights
Why do the child books get deleted when the author is destroyed?
Because dependent: :destroy tells Rails to delete all associated child records before deleting the parent, as shown in execution_table step 3 and 4.
What happens to child records with dependent: :nullify?
Instead of deleting children, Rails sets their foreign key to null, keeping the child records but removing the link to the parent.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what happens to the child books?
ATheir foreign keys are set to null
BThey remain unchanged
CThey are deleted
DThey are duplicated
💡 Hint
Check the 'Result' column at step 4 showing 'All 3 books deleted'
At which step does the parent author get deleted?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the 'Action' and 'Result' columns in step 5
If dependent: :nullify was used instead of :destroy, what would happen to the books after author deletion?
ABooks would remain with author_id set to null
BBooks would remain unchanged with author_id still set
CBooks would be deleted
DBooks would be duplicated
💡 Hint
Refer to key_moments explanation about dependent: :nullify behavior
Concept Snapshot
dependent: :destroy deletes all child records when parent is deleted.
dependent: :nullify keeps child records but sets their foreign keys to null.
Use dependent options to control child record cleanup.
Deleting parent triggers dependent action automatically.
Choose destroy to remove children, nullify to keep them without parent link.
Full Transcript
In Rails, when you delete a parent record, you can control what happens to its child records using dependent options. With dependent: :destroy, deleting the parent deletes all its children. With dependent: :nullify, deleting the parent keeps the children but removes their link by setting foreign keys to null. The execution table shows step-by-step how deleting an author with dependent: :destroy deletes all their books first, then deletes the author. Variable tracking confirms the author and books no longer exist after deletion. This helps keep your database clean and consistent automatically.