Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a callback that runs after a comment is created.
Ruby on Rails
class Comment < ApplicationRecord belongs_to :post after_[1] :notify_post_author def notify_post_author # notification logic here end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'after_save' which runs on both create and update, not just creation.
Using 'after_destroy' which runs after deletion, not creation.
✗ Incorrect
The 'after_create' callback runs only after a new record is created, which is suitable for notifying after a comment is added.
2fill in blank
mediumComplete the code to run a callback before a post is destroyed to clean up associated comments.
Ruby on Rails
class Post < ApplicationRecord has_many :comments, dependent: :destroy before_[1] :cleanup_comments def cleanup_comments comments.destroy_all end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'before_save' which runs on create and update, not on destroy.
Using 'after_destroy' which runs after deletion, too late for cleanup.
✗ Incorrect
The 'before_destroy' callback runs before the post is deleted, allowing cleanup of associated comments.
3fill in blank
hardFix the error in the callback method name to correctly run after a post's comments are added.
Ruby on Rails
class Post < ApplicationRecord has_many :comments, after_add: :[1] def log_comment_addition(comment) # log logic here end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not match the callback symbol.
Misnaming the callback method causing it not to run.
✗ Incorrect
The 'after_add' callback for the comments association runs after a comment is added. The method receives the added comment as an argument. The method name must match the symbol passed.
4fill in blank
hardFill both blanks to define a callback that runs before adding a tag to a post and calls the correct method.
Ruby on Rails
class Post < ApplicationRecord has_many :tags, [1]: :[2] def check_tag_limit(tag) # limit check logic end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'after_add' instead of 'before_add' for pre-add checks.
Mismatching method names causing callbacks not to trigger.
✗ Incorrect
The 'before_add' callback for the tags association runs before a tag is added, and the method 'check_tag_limit' (receiving the tag) is the correct handler.
5fill in blank
hardFill all three blanks to define callbacks that run before adding and after removing categories from a product.
Ruby on Rails
class Product < ApplicationRecord has_many :categories, [1]: :[2], [3]: :log_category_removal def log_category_addition(category) # log addition end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'before_remove' instead of 'after_remove' for post-removal actions.
Mismatching method names causing callbacks not to execute.
✗ Incorrect
The 'before_add' callback for the categories association runs before adding a category and calls 'log_category_addition(category)'. The 'after_remove' callback runs after removing a category and calls 'log_category_removal(category)'.