0
0
Ruby on Railsframework~20 mins

Association callbacks in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Association Callbacks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a child record is destroyed with dependent: :destroy?

Consider a Rails model Author has_many books with dependent: :destroy. What happens when an Author is destroyed?

Ruby on Rails
class Author < ApplicationRecord
  has_many :books, dependent: :destroy
end

class Book < ApplicationRecord
  belongs_to :author
  before_destroy :log_destroy

  def log_destroy
    puts "Destroying book #{id}"
  end
end

# In console:
author = Author.find(1)
author.destroy
AThe author is destroyed first, then all associated books are destroyed without running callbacks.
BThe author is destroyed and associated books remain in the database with author_id set to NULL.
CAll associated books are destroyed and their before_destroy callbacks run before the author is destroyed.
DDestroying the author raises an error because dependent: :destroy is not supported.
Attempts:
2 left
💡 Hint

Think about the order of callbacks and what dependent: :destroy means.

lifecycle
intermediate
2:00remaining
When is the after_add callback triggered in a has_many association?

Given a has_many :comments, after_add: :notify_addition association, when does the notify_addition method get called?

Ruby on Rails
class Post < ApplicationRecord
  has_many :comments, after_add: :notify_addition

  def notify_addition(comment)
    puts "Added comment #{comment.id}"
  end
end

post = Post.find(1)
comment = Comment.new(content: 'Nice post!')
post.comments << comment
AOnly after the comment is saved to the database and associated with the post.
BImmediately after a comment is added to the association collection in memory, before saving to the database.
CWhen the post itself is saved, regardless of changes to comments.
DOnly when the comment is destroyed from the association.
Attempts:
2 left
💡 Hint

Consider when the callback triggers relative to adding objects to the association.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a before_remove callback in a has_many association?

Choose the correct syntax to define a before_remove callback on a has_many :tags association that calls log_removal.

Ruby on Rails
class Article < ApplicationRecord
  has_many :tags, ???

  def log_removal(tag)
    puts "Removing tag #{tag.id}"
  end
end
Abefore_remove: :log_removal
Bbefore_remove => :log_removal
Cbefore_remove :log_removal
Dbefore_remove = :log_removal
Attempts:
2 left
💡 Hint

Look for the correct Ruby hash syntax for options in associations.

🔧 Debug
advanced
2:00remaining
Why does the after_add callback not trigger when using update to change association?

Given this code:

post.comments << comment
post.update(comments: post.comments + [new_comment])

The after_add callback on comments does not run for new_comment. Why?

ABecause <code>new_comment</code> is not saved before adding to the association.
BBecause <code>after_add</code> only triggers on <code>delete</code> operations, not additions.
CBecause <code>after_add</code> only triggers on <code>build</code> method calls.
DBecause <code>update</code> replaces the association without triggering callbacks like <code>after_add</code>.
Attempts:
2 left
💡 Hint

Think about how callbacks are triggered when modifying associations directly vs. via update.

🧠 Conceptual
expert
3:00remaining
What is the effect of using :before_add and :after_remove callbacks together on a has_many association?

In a has_many :items, before_add: :check_limit, after_remove: :log_removal association, what is the combined effect of these callbacks?

ABefore adding an item, <code>check_limit</code> runs to possibly prevent addition; after removing an item, <code>log_removal</code> runs to record the removal.
BBoth callbacks run only after the parent object is saved, not during association changes.
CThe callbacks run only if the associated items are destroyed, not when added or removed from the collection.
DThe callbacks run in reverse order: <code>log_removal</code> before removal and <code>check_limit</code> after addition.
Attempts:
2 left
💡 Hint

Consider the timing and purpose of before_add and after_remove callbacks.