Declaring ActiveRecord observers


#1

Hello

I’m using the ‘act_as_authenticated’ plugin to implement a small and
simple authentication system in my app. Everything is working just
fine and the world is a better place to live in. The plugin creates a
ActiveRecord observer in order to send the newly registered user a
confirmation e-mail.

Per the API docs [1], one should declare the observer in the
config/environment.rb file under the ‘config.active_record.observers =
:comment_observer, :signup_observer’.

But reading the generated source code, the plugin author recommends
declaring the observer in my ApplicationController with a ‘observer
:user_observer’ clause.

The observer gets triggered in both cases, but I am curious about this,

So, what is the “Rails way” of activating observers?

  1. http://rubyonrails.org/api/classes/ActiveRecord/Observer.html


#2

On 3/4/06, Juan Lupión removed_email_address@domain.invalid wrote:

:comment_observer, :signup_observer’.

  1. http://rubyonrails.org/api/classes/ActiveRecord/Observer.html

Using config.active_record.observers ensures the Observer is always
loaded. It’s actually probably a better way to go, because you don’t
have to make sure to load it when in script/console. (On the
flipside, it’s always running… so creating users from script/console
will send the emails out).

The only reason I don’t use it is because it didn’t seem to work
correctly in dev mode. They would work the first request, but not
after because of the class reloading. This may have been fixed
though. Try it out and let me know if everything works fine.


Rick O.
http://techno-weenie.net


#3

The only reason I don’t use it is because it didn’t seem to work
correctly in dev mode. They would work the first request, but not
after because of the class reloading. This may have been fixed
though. Try it out and let me know if everything works fine.

Uhm… declaring the observer only in “config.active_record_observers”
shows some erratic behaviour in development mode, as you said. So my
previous statement “The observer gets triggered in both cases” was
plain wrong, I guess.