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