Need help with rails-observers?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

489 Stars 112 Forks MIT License 157 Commits 20 Opened issues


Rails observer (removed from core in Rails 4.0)

Services available


Need anything else?

Contributors list

Build Status


Rails Observers (removed from core in Rails 4.0)


Add this line to your application's Gemfile:

gem 'rails-observers'
And then execute:
$ bundle

Or install it yourself as:

$ gem install rails-observers


This gem contains two observers:

  • Active Record Observer
  • Action Controller Sweeper

Active Record Observer

Observer classes respond to life cycle callbacks to implement trigger-like behavior outside the original class. This is a great way to reduce the clutter that normally comes when the model class is burdened with functionality that doesn't pertain to the core responsibility of the class. Observers are put in

). Example:
class CommentObserver < ActiveRecord::Observer
  def after_save(comment)
    Notifications.comment("[email protected]", "New comment was posted", comment).deliver

This Observer sends an email when a Comment#save is finished.

class ContactObserver < ActiveRecord::Observer
  def after_create(contact)'New contact added!')

def after_destroy(contact) contact.logger.warn("Contact with an id of #{} was destroyed!") end end

This Observer uses logger to log when specific callbacks are triggered.

The convention is to name observers after the class they observe. If you absolutely need to override this, or want to use one observer for several classes, use

class NotificationsObserver < ActiveRecord::Observer
  observe :comment, :like

def after_create(record) # notifiy users of new comment or like end


Please note that observers are called in the order that they are defined. This means that callbacks in an observer will always be called after callbacks defined in the model itself. Likewise,

use callbacks to enforce
dependent: :destroy
. Therefore, associated records will be destroyed before the observer's
is called.

For an observer to be active, it must be registered first. This can be done by adding the following line into the

    config.active_record.observers = :contact_observer

Enable multiple observers using array:

    config.active_record.observers = [:contact_observer, :user_observer]

Observers can also be registered on an environment-specific basis by simply using the corresponding environment's configuration file instead of


Action Controller Sweeper

Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change. They do this by being half-observers, half-filters and implementing callbacks for both roles. A Sweeper example:

class ListSweeper < ActionController::Caching::Sweeper
  observe List, Item

def after_save(record) list = record.is_a?(List) ? record : record.list expire_page(controller: "lists", action: %w( show public feed ), id: expire_action(controller: "lists", action: "all") list.shares.each { |share| expire_page(controller: "lists", action: "show", id: share.url_key) } end end

The sweeper is assigned in the controllers that wish to have its job performed using the

class method:
class ListsController < ApplicationController
  caches_action :index, :show, :public, :feed
  cache_sweeper :list_sweeper, only: [ :edit, :destroy, :share ]

In the example above, four actions are cached and three actions are responsible for expiring those caches.

You can also name an explicit class in the declaration of a sweeper, which is needed if the sweeper is in a module:

class ListsController < ApplicationController
  caches_action :index, :show, :public, :feed
  cache_sweeper OpenBar::Sweeper, only: [ :edit, :destroy, :share ]


  1. Fork it
  2. Create your feature branch (
    git checkout -b my-new-feature
  3. Commit your changes (
    git commit -am 'Added some feature'
  4. Push to the branch (
    git push origin my-new-feature
  5. Create new Pull Request

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.