0
0
Ruby on Railsframework~10 mins

Callbacks overview in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a callback that runs before saving a record.

Ruby on Rails
class User < ApplicationRecord
  [1] :normalize_name

  def normalize_name
    self.name = name.downcase.titleize
  end
end
Drag options to blanks, or click blank then click option'
Aaround_update
Bafter_create
Cbefore_save
Dafter_destroy
Attempts:
3 left
💡 Hint
Common Mistakes
Using after_create runs too late, after the record is created.
around_update is for wrapping update actions, not saving.
after_destroy runs after deletion, not saving.
2fill in blank
medium

Complete the code to run a callback after a record is created.

Ruby on Rails
class Order < ApplicationRecord
  [1] :send_confirmation_email

  def send_confirmation_email
    # email sending logic
  end
end
Drag options to blanks, or click blank then click option'
Aafter_create
Bbefore_save
Cbefore_destroy
Daround_save
Attempts:
3 left
💡 Hint
Common Mistakes
before_save runs too early, before creation.
before_destroy is for deletion, not creation.
around_save wraps save, not specifically creation.
3fill in blank
hard

Fix the error in the callback declaration to run a method before updating a record.

Ruby on Rails
class Product < ApplicationRecord
  [1] :check_stock

  def check_stock
    # stock checking logic
  end
end
Drag options to blanks, or click blank then click option'
Abefore_update
Bafter_update
Cafter_save
Dbefore_create
Attempts:
3 left
💡 Hint
Common Mistakes
after_update runs too late, after the update.
after_save runs after any save, not just update.
before_create runs before creating, not updating.
4fill in blank
hard

Fill both blanks to define callbacks that run before creating and after destroying a record.

Ruby on Rails
class Comment < ApplicationRecord
  [1] :sanitize_content
  [2] :log_deletion

  def sanitize_content
    # clean content
  end

  def log_deletion
    # log deletion
  end
end
Drag options to blanks, or click blank then click option'
Abefore_create
Bafter_create
Cafter_destroy
Dbefore_update
Attempts:
3 left
💡 Hint
Common Mistakes
Using after_create instead of before_create for sanitizing.
Using before_update instead of after_destroy for logging deletion.
5fill in blank
hard

Fill all three blanks to define callbacks that run before validation, after save, and around update.

Ruby on Rails
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
Drag options to blanks, or click blank then click option'
Abefore_validation
Bafter_save
Caround_update
Dbefore_save
Attempts:
3 left
💡 Hint
Common Mistakes
Using before_save instead of before_validation for preparing data.
Using before_save instead of after_save for clearing cache.
Confusing around_update with before_update or after_update.