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?✗ Incorrect
dependent: :nullify sets the foreign key in child records to NULL, keeping the child records but removing their link to the parent.
Which
dependent option runs callbacks on child records before deletion?✗ Incorrect
dependent: :destroy runs callbacks on each child record before deleting it.
If you want to keep child records but remove their link to the parent, which option do you use?
✗ Incorrect
dependent: :nullify keeps child records but sets their foreign key to NULL.
What risk exists if you omit the
dependent option in a has_many association?✗ Incorrect
Without dependent, deleting a parent leaves child records with foreign keys pointing to a non-existent parent, causing orphaned records.
Which
dependent option deletes child records directly in the database without callbacks?✗ Incorrect
dependent: :delete_all deletes child records directly in the database, skipping callbacks.
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.