Callbacks in Ruby

From the Ruby on Rails cheat sheet · Models & Active Record · verified Jul 2026

Callbacks

Lifecycle hooks for model operations

ruby
class Post < ApplicationRecord
  before_validation :normalize_title
  before_save :generate_slug
  after_create :send_notification
  after_destroy :cleanup_files

  private

  def normalize_title
    self.title = title.strip.titleize
  end
end
✅ Callbacks run automatically during object lifecycle
💡 before_validation runs before validations
🔍 Use conditional callbacks with if/unless
⚠️ Avoid complex logic in callbacks, use service objects
modelscallbackslifecycle

Continue with Ruby on Rails

Save the full cheat sheet or work through every related task.

More Ruby tasks

Back to the full Ruby on Rails cheat sheet