Ruby on Rails Observer Implementation Part 2

16 April 2012 by Amar Daximi 1 comment

In my previous article i had gone through how ruby observer works if you haven’t read it check it out.

In this article I will go through how the rails observer works and how callback are notifying the observer.

I’ll be jumping through 2 files so keep it open, it will help you to understand the workflow of how rails’ observers work.

  1. Active Model Observing
  2. Active Record Observer

Consider having a contact and account model and adding observers to both models.

class AccountObserver < ActiveRecord::Observer
  observe :account,:contact
  def after_create(record)
   # do some stuff
   # ......
   # do some stuff
# Add to config/application.rb
config.active_record.observers = :account_observer
  1. When rails boots ,the observer is initialized by calling AR::Base.instantiate_observers in AR Railties.
  2. It calls the instantiate_observers of active model’s observing module.
    • Which creates single instance of observer class by using the singleton module .
      eg. AccountObserver.instance
  3. After creation the instance is initialized. It does the following things.
    • Finds all observable classes (:account,:contact)
    • Adds the observer to them
    • eg. Account.add_observer(AccountObserver.instance)
  4. So the first step is to get all Observable classes which we will get by calling observed_classes.
    observed_classes is overridden by AR::Observer

    • It gets all observable classes by calling super. It will get all classes via the observe method which is explicitly defined or by the class name of observer.
    • If there are any subclasses (descendants) classes it will also observed too.
    • eg. Account,Contact
  5. Now after getting all classes the observer is added this can be done by calling add_observer!(klass)
    add_observer!(klass) is again overriden by AR::Observer

    • It adds observer to all observable class by calling super
    • And then it calls define_callbacks method which is present in AR::Observer
  6. define_callbacks method is responsible for adding notification support for observable class
    • It traverses all AR::Callbacks and if observer method matches AR::Callback method then
      it creates a dynamic notification method which would look like _notify_#{observer_name}_for_#{callback}
    • This method is added to observable class(Account) and its called on the callback
    • eg. after_create :_notify_account_observer_for_after_create
  7. When the callback is invoked (in our case after_create) it will call its respective notification method
    • Above notification method has following definition.
    • Update method again check callback method is present in Observer instance if yes then it call callback method of Observer class
    • eg.

      it check and call after_create of AccountObserver class.

I hope it is helpful to understand how rails observer is implemented.

Comments and feedbacks are welcome.

Amar Daximi

Follow me on Twitter

1 thought on “Ruby on Rails Observer Implementation Part 2”

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Blog

Get access to proven marketing ideas, latest trends and best practices.

Next up home


Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)