Forum: Ruby on Rails Observer best practices

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jose Hales-Garcia (Guest)
on 2006-04-27 02:09
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
Robby R. (Guest)
on 2006-04-27 04:48
(Received via mailing list)
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/wh...

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

2) Modify config/environment.rb

config.active_record.observers = :custom_observer

3) have fun!

Hope that helps!

Robby


Robby R.
Founder & Executive Director

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

www.planetargon.com
www.robbyonrails.com

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4968 [fax]
This topic is locked and can not be replied to.