`

# Difference between Observers and callbacks?

199

In Ruby on Rails, Observers and Callbacks serve different purposes and are used in different contexts to achieve similar ends: reacting to changes in the state of objects. Here's a detailed comparison of both:

Callbacks

Purpose: Callbacks are methods that get called at certain points in an object's lifecycle. They are typically used to trigger logic that should run before or after events such as object creation, updating, saving, and destroying.

How they work:
- Callbacks are defined directly within the model class.
- They can be set up to run before or after specific actions such as `before_save`, `after_create`, `before_destroy`, etc.

Usage:
class User < ApplicationRecord
  before_save :normalize_name

  private

  def normalize_name
    self.name = name.downcase.titleize
  end
end

Advantages:
- Simple to implement.
- Code is located within the model, making it easy to see all logic related to the model in one place.

Disadvantages:
- Can lead to bloated models if too many callbacks are defined.
- Mixing business logic with model code can make the code harder to maintain.

Observers

Purpose: Observers are used to keep the models lean by moving callback-like behavior into separate classes. They are useful for keeping business logic out of the model classes, promoting cleaner, more maintainable code.

How they work:
- Observers are separate classes that observe events on a model.
- They are registered to listen to lifecycle events like `after_save`, `before_update`, etc.

Usage:
1. Create an observer class:
    class UserObserver < ActiveRecord::Observer
      def after_create(user)
        # Logic to run after a user is created
        UserMailer.welcome_email(user).deliver
      end
    end
2. Register the observer in the application configuration:
    # config/application.rb
    config.active_record.observers = :user_observer

Advantages:
- Keeps models lean by moving callback logic to separate classes.
- Promotes separation of concerns and cleaner code.

Disadvantages:
- Introduces indirection, making it harder to trace what happens when a model is changed.
- Observers are less commonly used and known, which might affect code readability for teams not familiar with the pattern.

Key Differences

- Location of Logic: Callbacks are defined within the model, while observers are defined in separate classes.
- Separation of Concerns: Observers promote separation of concerns by decoupling business logic from models, whereas callbacks can lead to models containing both data and behavior.
- Usage and Popularity: Callbacks are more straightforward and commonly used in Rails applications, whereas observers, although useful for keeping code clean, are less commonly used and may require more setup and understanding.

When to Use Each


- Callbacks are best when you have simple logic that is closely related to the model's lifecycle and doesn't add significant complexity to the model.
- Observers are ideal when you have more complex business logic that you want to keep out of the model to maintain separation of concerns and improve code maintainability.

Choosing between observers and callbacks depends on the specific requirements of your application and the complexity of the logic you need to implement.