💎 Ruby on Rails
Advanced
What is the Observer pattern in Rails and is it still used?
Answer
Rails once included ActiveRecord::Observer — a way to watch model lifecycle events from a separate class, reducing callback clutter in models. Example: class UserObserver < ActiveRecord::Observer; def after_create(user); Mailer.welcome(user).deliver; end; end. Observers were extracted to the rails-observers gem in Rails 4 and are no longer in Rails core. Modern alternatives: use callbacks sparingly in models (only for intrinsic model concerns), service objects for cross-cutting operations, event systems (Wisper, dry-events) for decoupled pub/sub, or ActiveSupport::Notifications for instrumenting and subscribing to app events.
Previous
What is database sharding in the context of Rails?
Next
What is the role of Puma in Rails deployment?
More Ruby on Rails Questions
View all →- Advanced What is metaprogramming in Ruby/Rails and give examples?
- Advanced What is the difference between include, extend, and prepend in Ruby?
- Advanced How does Rails handle database connection pooling?
- Advanced What is Rack and how does Rails relate to it?
- Advanced What is query optimization with explain in Rails?