Observer best practices

I’ve seen two ways of connecting observers:

  1. In the observer class put

    observe MyModel

  2. In the controller class put

    observer :my_observer

What is the difference between these two? Also, is it necessary to put
in the observer class the following?

  MyObserver.instance

Jose

On Apr 26, 2006, at 3:09 PM, Jose Hales-Garcia wrote:

What is the difference between these two? Also, is it necessary to
put
in the observer class the following?

  MyObserver.instance

Jose

Don’t go the controller route… that doesn’t really work when you’re
doing stuff that isn’t through your controllers.

I posted about this a while back on my blog:

http://www.robbyonrails.com/articles/2006/02/27/where-did-my-observer-go

You can do the following:

  1. create the observer:

app/models/custom_observer.rb

class CustomObserver < ActiveRecord::Observer

showing how you can use Camel or symbols

observe Contact, :contact_note

def after_create( object )
# do something… with object
end
end

  1. Modify config/environment.rb

config.active_record.observers = :custom_observer

  1. have fun!

Hope that helps!

Robby

Robby R.
Founder & Executive Director

PLANET ARGON, LLC
Ruby on Rails Development, Consulting & Hosting

www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]