0
0
Ruby on Railsframework~20 mins

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

Choose your learning style9 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.