Edge Rails: observer is deprecated

I’m getting the dreaded “DEPRECATION WARNING: observer is deprectaed”
notice when running functional tests. I use an observer in a controller
to instantiate a a UserObserver object to observe changes to my User
model. This is documented on page 380 of the Agile Web D. with
Rails (Second Edition). If this use is indeed deprecated, what is the
appropriate way to instantite an observer from a controller?

Gregg

I’m using an Observer as well, using that same example (well, from the
2nd edition of that book) and I’m getting the same warning. It goes
on: “See Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern. for details,” but
there’s really nothing on that web page but a coming-soon message.

Below is the whole message I get, with a second deprecation warning
that seems related, and so I was also wondering if anybody knew what is
going on with Observer and why this change is being made?

There is another way to implement an observer, calling instance:
MyObserver.instance

But that requires initializing it somewhere, which is what the observer
method of ActionController was doing…

Anybody?

This is a really useful facility, it would be a shame to strip it from
Rails 2.0 if there isn’t a compelling replacement.

-Billy

DEPRECATION WARNING: observer is deprecated and will be removed from
Rails 2.0 See Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern. for details.
(called from
./script/…/config/…/app/controllers/things_controller.rb:3)
DEPRECATION WARNING: depend_on is deprecated and will be removed from
Rails 2.0 See Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern. for details.
(called from observer_without_deprecation at
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/deprecated_dependencies.rb:29)

I’m using an Observer as well, using that same example (well, from the
2nd edition of that book) and I’m getting the same warning. It goes
on: “See Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern. for details,” but
there’s really nothing on that web page but a coming-soon message.

Set config.observers in your environment.rb.

config.observers = [:foo_observer]


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com

Thanks for the tip, but I get an error when trying this:

…/config/environment.rb:41: undefined method `observers=’ for
#Rails::Configuration:0x3128cc (NoMethodError)

I don’t see anything in railties/lib/environment.rb to support this.
Also, I found some references (from October) about this mechanism being
incompatible with Mongrel, although that may have since been resolved.
(http://rubyforge.org/pipermail/mongrel-users/2006-October/001927.html)

Gregg
http://www.kellogg-assoc.com

Thanks! That did the trick. Even though it’s there in the config file,
it was almost impossible to find in the online documentation. The
online API documentation doesn’t include Rails::Configuration (not in
http://api.rubyonrails.org/ anyway). Searching for it gives the cryptic
notice that config.active_record is “A stub for setting options on
ActiveRecord::Base” which can be infered from
ActiveRecord::Observing::ClassMethods (
http://railsmanual.org/module/ActiveRecord%3A%3AObserving%3A%3AClassMethods
). Still, not obvious.

Perhaps the deprecation page, whenever it comes about, will make this
clear. Also, too bad they didn’t get this into the Agile v2 book.

Gregg
www.kellogg-assoc.com

On 1/26/07, Gregg [email protected] wrote:

Thanks for the tip, but I get an error when trying this:

…/config/environment.rb:41: undefined method `observers=’ for
#Rails::Configuration:0x3128cc (NoMethodError)

I don’t see anything in railties/lib/environment.rb to support this.
Also, I found some references (from October) about this mechanism being
incompatible with Mongrel, although that may have since been resolved.
(http://rubyforge.org/pipermail/mongrel-users/2006-October/001927.html)

DOH!

config.active_record.observers = …

There is an example in the default environment.rb:

http://dev.rubyonrails.org/browser/tags/rel_1-2-1/railties/environments/environment.rb#L38

That issue you linked to has been resolved. The problem was, it would
load your models right away, and bomb if your models used some plugin
features that hadn’t been loaded yet. Observers now load after
plugins.

Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com