Complete the code to define a callback that runs before saving a record.
class User < ApplicationRecord [1] :normalize_name def normalize_name self.name = name.downcase.titleize end end
The before_save callback runs just before the record is saved, allowing you to modify attributes.
Complete the code to run a callback after a record is created.
class Order < ApplicationRecord [1] :send_confirmation_email def send_confirmation_email # email sending logic end end
The after_create callback runs after a new record is successfully created.
Fix the error in the callback declaration to run a method before updating a record.
class Product < ApplicationRecord [1] :check_stock def check_stock # stock checking logic end end
The before_update callback runs just before an existing record is updated.
Fill both blanks to define callbacks that run before creating and after destroying a record.
class Comment < ApplicationRecord [1] :sanitize_content [2] :log_deletion def sanitize_content # clean content end def log_deletion # log deletion end end
before_create runs before a new record is created, and after_destroy runs after a record is deleted.
Fill all three blanks to define callbacks that run before validation, after save, and around update.
class Article < ApplicationRecord [1] :prepare_data [2] :clear_cache [3] :track_changes def prepare_data # prepare data before validation end def clear_cache # clear cache after save end def track_changes # track changes around update end end
before_validation runs before validations, after_save runs after saving, and around_update wraps the update process.