Dependent destroy and nullify help manage related data automatically when you delete a record. They keep your database clean and avoid broken links.
Dependent destroy and nullify in Ruby on Rails
has_many :associated_records, dependent: :destroy has_many :associated_records, dependent: :nullify
dependent: :destroy deletes all associated records when the parent is deleted.
dependent: :nullify sets the foreign key in associated records to null instead of deleting them.
class User < ApplicationRecord
has_many :posts, dependent: :destroy
endcategory_id in products is set to null, keeping the products.class Category < ApplicationRecord
has_many :products, dependent: :nullify
endThis example shows a user with two posts. When the user is deleted, all their posts are deleted automatically because of dependent: :destroy.
class User < ApplicationRecord has_many :posts, dependent: :destroy end class Post < ApplicationRecord belongs_to :user end # In Rails console or test user = User.create(name: "Alice") post1 = user.posts.create(title: "Hello") post2 = user.posts.create(title: "World") puts Post.count # => 2 user.destroy puts Post.count # => 0
Use dependent: :destroy carefully because it deletes data permanently.
dependent: :nullify keeps data but removes the link, so make sure your app handles null foreign keys.
These options help keep your database consistent without extra code.
dependent: :destroy deletes associated records when the parent is deleted.
dependent: :nullify sets foreign keys to null instead of deleting associated records.
They help manage related data automatically and keep your database clean.
Practice
dependent: :destroy do in a Rails model association?Solution
Step 1: Understand the role of dependent option
Thedependentoption in Rails associations controls what happens to associated records when the parent is deleted.Step 2: Identify behavior of
The:destroy:destroyoption deletes all associated records automatically when the parent record is deleted.Final Answer:
Deletes associated records when the parent record is deleted. -> Option AQuick Check:
dependent: :destroy = deletes associated records [OK]
- Confusing :destroy with :nullify behavior
- Thinking :destroy prevents parent deletion
- Assuming it backs up records before deletion
Solution
Step 1: Recall valid dependent options in Rails
Rails supports:destroy,:delete_all, and:nullifyas valid dependent options.Step 2: Identify the option that sets foreign keys to null
dependent: :nullifysets the foreign keys of associated records tonullinstead of deleting them.Final Answer:
has_many :comments, dependent: :nullify -> Option CQuick Check:
dependent: :nullify = sets foreign keys to null [OK]
- Using :destroy_all which is invalid
- Confusing :delete_all with :nullify
- Using non-existent :remove option
class Post < ApplicationRecord has_many :comments, dependent: :nullify end class Comment < ApplicationRecord belongs_to :post, optional: true end
What happens to comments when a post is deleted?
Solution
Step 1: Analyze the dependent option in Post model
Thedependent: :nullifyoption means when a post is deleted, associated comments' foreign keys (post_id) are set tonull.Step 2: Check if comments can have null
Thepost_idbelongs_to :post, optional: trueallows comments to exist without a post, so settingpost_idto null is valid.Final Answer:
Comments remain but their post_id is set to null. -> Option AQuick Check:
dependent: :nullify + optional: true = nullify foreign keys [OK]
- Assuming comments get deleted with post
- Ignoring optional: true and expecting errors
- Thinking comments keep old post_id after deletion
class User < ApplicationRecord has_many :posts, dependent: :nullify end class Post < ApplicationRecord belongs_to :user end
Solution
Step 1: Understand dependent: :nullify effect
It sets foreign keys of associated posts to null when a user is deleted.Step 2: Check belongs_to association in Post
belongs_to :userby default requiresuser_idto be present (not null). Since it's not marked optional, nullifyinguser_idcauses validation or DB errors.Final Answer:
Posts cannot have null user_id because belongs_to is not optional. -> Option DQuick Check:
dependent: :nullify requires optional belongs_to [OK]
- Assuming nullify works without optional: true
- Thinking dependent: :nullify is invalid syntax
- Confusing belongs_to with has_many in Post
Library model with many Books. You want to delete a library and automatically delete all its books. But if a Book has Reviews, you want to keep the reviews but remove the book reference (set book_id to null). Which setup correctly achieves this?Solution
Step 1: Set Library to delete all books on deletion
Usingdependent: :destroyonhas_many :booksdeletes all books when a library is deleted.Step 2: Set Book to nullify reviews instead of deleting
Usingdependent: :nullifyonhas_many :reviewskeeps reviews but sets theirbook_idto null.Step 3: Allow reviews to have null book_id
Markingbelongs_to :book, optional: truein Review allowsbook_idto be null without errors.Final Answer:
Library has_many :books, dependent: :destroy; Book has_many :reviews, dependent: :nullify; Review belongs_to :book, optional: true -> Option BQuick Check:
destroy deletes children; nullify clears foreign keys with optional true [OK]
- Using nullify on library-books deletes nothing
- Destroying reviews deletes data you want to keep
- Not setting optional: true on Review's belongs_to
Ready to prove it?
Pass the test and this pattern moves unknown
