Bird
Raised Fist0
Ruby on Railsframework~20 mins

Dependent destroy and nullify in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Dependent Destroy and Nullify
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a parent record is destroyed with dependent: :destroy?
Consider a Rails model Author that has_many :books, dependent: :destroy. What happens to the associated books when an Author is deleted?
Ruby on Rails
class Author < ApplicationRecord
  has_many :books, dependent: :destroy
end

class Book < ApplicationRecord
  belongs_to :author
end

# In Rails console:
author = Author.find(1)
author.destroy
AAll associated books have their author_id set to NULL but remain in the database.
BAll associated books are deleted from the database automatically.
CThe author record is deleted but the books remain unchanged with their author_id intact.
DAn error is raised because dependent: :destroy requires manual deletion of books.
Attempts:
2 left
💡 Hint
Think about what dependent: :destroy means for child records when the parent is removed.
❓ component_behavior
intermediate
2:00remaining
What is the effect of dependent: :nullify on associated records?
Given a Rails model Category that has_many :products, dependent: :nullify, what happens to the products when a Category is destroyed?
Ruby on Rails
class Category < ApplicationRecord
  has_many :products, dependent: :nullify
end

class Product < ApplicationRecord
  belongs_to :category, optional: true
end

# In Rails console:
category = Category.find(1)
category.destroy
AAll associated products have their category_id set to NULL but remain in the database.
BAll associated products are deleted from the database.
CThe category record is deleted but products keep their category_id pointing to the deleted record.
DAn error is raised because dependent: :nullify requires products to be deleted manually.
Attempts:
2 left
💡 Hint
Think about what nullify means for foreign keys when the parent is removed.
🔧 Debug
advanced
3:00remaining
Why does dependent: :destroy cause a stack level too deep error in a self-referential association?
Consider a Rails model Comment with a self-referential association:
has_many :replies, class_name: 'Comment', foreign_key: 'parent_id', dependent: :destroy
and
belongs_to :parent, class_name: 'Comment', optional: true.
Destroying a comment with replies causes a SystemStackError: stack level too deep. Why?
Ruby on Rails
class Comment < ApplicationRecord
  has_many :replies, class_name: 'Comment', foreign_key: 'parent_id', dependent: :destroy
  belongs_to :parent, class_name: 'Comment', optional: true
end

comment = Comment.find(1)
comment.destroy
ABecause the replies are not loaded before destroy, causing a nil error.
BBecause Rails does not support dependent: :destroy on self-referential associations.
CBecause the foreign key parent_id is not nullable, causing a database constraint error.
DBecause dependent: :destroy recursively destroys replies, which in turn try to destroy their parent again, causing infinite recursion.
Attempts:
2 left
💡 Hint
Think about what happens when destroying nested child records that point back to the parent.
📝 Syntax
advanced
1:30remaining
Which option correctly sets up dependent: :nullify in a Rails model?
You want to set up a has_many association where deleting the parent sets the child's foreign key to NULL. Which code is correct?
Ahas_many :comments, dependent: :delete_nullify
Bhas_many :comments, dependent: :destroy_nullify
Chas_many :comments, dependent: :nullify
Dhas_many :comments, dependent: :nullify_destroy
Attempts:
2 left
💡 Hint
Check the exact symbol Rails expects for nullifying foreign keys on dependent destroy.
❓ state_output
expert
3:00remaining
What is the state of child records after destroying a parent with dependent: :destroy and callbacks?
Given these models:
class Post < ApplicationRecord has_many :comments, dependent: :destroy end
class Comment < ApplicationRecord belongs_to :post before_destroy { puts "Destroying comment #{id}" } end
When post.destroy is called, what is the output and final state of comments?
Ruby on Rails
post = Post.find(1)
post.destroy
AConsole prints "Destroying comment X" for each comment, and all comments are deleted from the database.
BConsole prints nothing, and comments remain in the database with post_id set to NULL.
CConsole prints "Destroying comment X" for each comment, but comments remain in the database.
DAn error is raised because before_destroy callbacks prevent deletion.
Attempts:
2 left
💡 Hint
dependent: :destroy triggers callbacks on child records before deleting them.

Practice

(1/5)
1. What does dependent: :destroy do in a Rails model association?
easy
A. Deletes associated records when the parent record is deleted.
B. Sets associated records' foreign keys to null when the parent is deleted.
C. Prevents deletion of the parent record if associated records exist.
D. Copies associated records to a backup table before deletion.

Solution

  1. Step 1: Understand the role of dependent option

    The dependent option in Rails associations controls what happens to associated records when the parent is deleted.
  2. Step 2: Identify behavior of :destroy

    The :destroy option deletes all associated records automatically when the parent record is deleted.
  3. Final Answer:

    Deletes associated records when the parent record is deleted. -> Option A
  4. Quick Check:

    dependent: :destroy = deletes associated records [OK]
Hint: Destroy deletes children; nullify just clears foreign keys [OK]
Common Mistakes:
  • Confusing :destroy with :nullify behavior
  • Thinking :destroy prevents parent deletion
  • Assuming it backs up records before deletion
2. Which is the correct syntax to set an association to nullify dependent records in Rails?
easy
A. has_many :comments, dependent: :delete_all
B. has_many :comments, dependent: :remove
C. has_many :comments, dependent: :nullify
D. has_many :comments, dependent: :destroy_all

Solution

  1. Step 1: Recall valid dependent options in Rails

    Rails supports :destroy, :delete_all, and :nullify as valid dependent options.
  2. Step 2: Identify the option that sets foreign keys to null

    dependent: :nullify sets the foreign keys of associated records to null instead of deleting them.
  3. Final Answer:

    has_many :comments, dependent: :nullify -> Option C
  4. Quick Check:

    dependent: :nullify = sets foreign keys to null [OK]
Hint: Use :nullify to keep records but clear foreign keys [OK]
Common Mistakes:
  • Using :destroy_all which is invalid
  • Confusing :delete_all with :nullify
  • Using non-existent :remove option
3. Given the models:
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?
medium
A. Comments remain but their post_id is set to null.
B. All comments are deleted from the database.
C. Comments remain unchanged with the same post_id.
D. An error occurs because comments must belong to a post.

Solution

  1. Step 1: Analyze the dependent option in Post model

    The dependent: :nullify option means when a post is deleted, associated comments' foreign keys (post_id) are set to null.
  2. Step 2: Check if comments can have null post_id

    The belongs_to :post, optional: true allows comments to exist without a post, so setting post_id to null is valid.
  3. Final Answer:

    Comments remain but their post_id is set to null. -> Option A
  4. Quick Check:

    dependent: :nullify + optional: true = nullify foreign keys [OK]
Hint: Nullify keeps records but clears foreign keys if optional true [OK]
Common Mistakes:
  • Assuming comments get deleted with post
  • Ignoring optional: true and expecting errors
  • Thinking comments keep old post_id after deletion
4. Identify the error in this Rails model code:
class User < ApplicationRecord
  has_many :posts, dependent: :nullify
end

class Post < ApplicationRecord
  belongs_to :user
end
medium
A. belongs_to should be has_many in Post model.
B. No error; code works as expected.
C. dependent: :nullify is invalid syntax for has_many.
D. Posts cannot have null user_id because belongs_to is not optional.

Solution

  1. Step 1: Understand dependent: :nullify effect

    It sets foreign keys of associated posts to null when a user is deleted.
  2. Step 2: Check belongs_to association in Post

    belongs_to :user by default requires user_id to be present (not null). Since it's not marked optional, nullifying user_id causes validation or DB errors.
  3. Final Answer:

    Posts cannot have null user_id because belongs_to is not optional. -> Option D
  4. Quick Check:

    dependent: :nullify requires optional belongs_to [OK]
Hint: Use optional: true with nullify to avoid errors [OK]
Common Mistakes:
  • Assuming nullify works without optional: true
  • Thinking dependent: :nullify is invalid syntax
  • Confusing belongs_to with has_many in Post
5. You have a 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?
hard
A. Library has_many :books, dependent: :destroy; Book has_many :reviews, dependent: :destroy; Review belongs_to :book, optional: true
B. Library has_many :books, dependent: :destroy; Book has_many :reviews, dependent: :nullify; Review belongs_to :book, optional: true
C. Library has_many :books, dependent: :nullify; Book has_many :reviews, dependent: :destroy; Review belongs_to :book
D. Library has_many :books, dependent: :nullify; Book has_many :reviews, dependent: :nullify; Review belongs_to :book

Solution

  1. Step 1: Set Library to delete all books on deletion

    Using dependent: :destroy on has_many :books deletes all books when a library is deleted.
  2. Step 2: Set Book to nullify reviews instead of deleting

    Using dependent: :nullify on has_many :reviews keeps reviews but sets their book_id to null.
  3. Step 3: Allow reviews to have null book_id

    Marking belongs_to :book, optional: true in Review allows book_id to be null without errors.
  4. Final Answer:

    Library has_many :books, dependent: :destroy; Book has_many :reviews, dependent: :nullify; Review belongs_to :book, optional: true -> Option B
  5. Quick Check:

    destroy deletes children; nullify clears foreign keys with optional true [OK]
Hint: Destroy parents, nullify grandchildren with optional belongs_to [OK]
Common Mistakes:
  • Using nullify on library-books deletes nothing
  • Destroying reviews deletes data you want to keep
  • Not setting optional: true on Review's belongs_to
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard