0
0
Ruby on Railsframework~5 mins

Dependent destroy and nullify in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does dependent: :destroy do in a Rails association?
It deletes all associated child records when the parent record is deleted, ensuring no orphaned records remain.
Click to reveal answer
beginner
What is the effect of dependent: :nullify in a Rails model association?
It sets the foreign key of associated child records to NULL instead of deleting them when the parent is deleted.
Click to reveal answer
intermediate
When should you use dependent: :destroy instead of dependent: :nullify?
Use destroy when child records should be removed with the parent to keep data clean. Use nullify when child records should remain but lose their link to the deleted parent.
Click to reveal answer
beginner
What happens if you don't specify any dependent option in a Rails association?
The associated child records remain unchanged when the parent is deleted, which can lead to orphaned records with invalid foreign keys.
Click to reveal answer
intermediate
Explain the difference between dependent: :destroy and dependent: :delete_all.
destroy runs callbacks on each child before deleting, while delete_all deletes all children directly in the database without callbacks.
Click to reveal answer
What does dependent: :nullify do when the parent record is deleted?
ASets child records' foreign keys to NULL
BDeletes all child records
CRaises an error
DDoes nothing
Which dependent option runs callbacks on child records before deletion?
Anullify
Bdestroy
Cdelete_all
Drestrict_with_exception
If you want to keep child records but remove their link to the parent, which option do you use?
Adependent: :restrict_with_error
Bdependent: :destroy
Cdependent: :delete_all
Ddependent: :nullify
What risk exists if you omit the dependent option in a has_many association?
AOrphaned child records may remain
BChild records are automatically deleted
CParent record cannot be deleted
DForeign keys are set to NULL
Which dependent option deletes child records directly in the database without callbacks?
Adestroy
Bnullify
Cdelete_all
Drestrict_with_exception
Describe how dependent: :destroy and dependent: :nullify affect child records when a parent record is deleted in Rails.
Think about whether child records are deleted or kept.
You got /4 concepts.
    Explain why it is important to use the dependent option in Rails associations.
    Consider what happens to child records when the parent is removed.
    You got /4 concepts.