Observers?


#1

Hello all!

I’m having an issue with Observers, and I’m hoping someone knows the
answer.

Here is the definition:

app/models/vehicle_observer.rb

class VehicleObserver < ActiveRecord::Observer
def after_save(vehicle)
breakpoint
if vehicle.has_new_mil_indication?
UserMailer.deliver_mil_notice(vehicle)
end
end
end

VehicleObserver.instance

It seems from reading AWDR that the last line may not be necessary
for Rails apps,
though it appears that if it isn’t there you can only specify when
observations
take place via the observer command in Controllers. I want to observe
ALL saves to
Vehicle, not just ones generated via a controller. In fact, the vast
majority of
saves to Vehicle happen in a background process outside the web
application.

My problem is that observations aren’t taking place. :slight_smile:

I never hit the breakpoint line, which I assume means there’s no
observing at all.

I’d expect the observer to get invoked during unit tests. Is there
any reason to
believe otherwise?

Thanks all!

P.S. I’m pretty sure I can just move this method into the model
itself and use the
hook interface, but I’m trying to understand observers and this
seems like a
good use of them.


– Tom M.


#2

On 3/9/06, Tom M. removed_email_address@domain.invalid wrote:

def after_save(vehicle)
for Rails apps,

itself and use the
hook interface, but I’m trying to understand observers and this
seems like a
good use of them.


– Tom M.

in environment.rb:

Activate observers that should always be running

config.active_record.observers = :cacher, :garbage_collector

The problem I’ve had with that, however, is that observer seem to
disappear after the first request in development mode. Perhaps use
this AND the observer class method of controllers.


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


#3

Thanks, Rick!

I just gave up and used hooks instead.

I don’t think the observers are intended to be used
outside of the context of a controller. Either that,
or they work in a way that I cannot understand.

I tried adding my class to the environment.rb where
you suggested, but the breakpoint was still never
reached by tests.

Weird.


– Tom M.


#4

Tom M. wrote:

Thanks, Rick!

I just gave up and used hooks instead.

I don’t think the observers are intended to be used
outside of the context of a controller. Either that,
or they work in a way that I cannot understand.

I tried adding my class to the environment.rb where
you suggested, but the breakpoint was still never
reached by tests.

Weird.


– Tom M.

Are you familiar with the Observer pattern? If you read up on it in,
“Headfirst Patterns” by O’Reilly, you’ll get a good idea of how to use
it correctly and that should help.

BTW, I have no affiation at all with the book or authors. It is just a
great read.


#5

Cody S. wrote:

Are you familiar with the Observer pattern? If you read up on it in,
“Headfirst Patterns” by O’Reilly, you’ll get a good idea of how to use
it correctly and that should help.

BTW, I have no affiation at all with the book or authors. It is just a
great read.

Sorry. Make that, “Head First Design Patterns”.

http://www.oreilly.com/catalog/hfdesignpat/index.html


#6

Thanks for the tip, Cody!

It sounds as though you have first hand experience of
observers in Rails. Do you know that they can work in
the ways that I described in my first post?


– Tom M.


#7

On 3/10/06, Tom M. removed_email_address@domain.invalid wrote:

Thanks for the tip, Cody!

It sounds as though you have first hand experience of
observers in Rails. Do you know that they can work in
the ways that I described in my first post?


– Tom M.

ActiveRecord::Base.observers = [:article_observer]

That config.active_record.observers snippet didnt seem to work for
some reason. Setting that in env.rb worked though.


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


#8

Tom M. wrote:

Thanks for the tip, Cody!

It sounds as though you have first hand experience of
observers in Rails. Do you know that they can work in
the ways that I described in my first post?


– Tom M.

HI Tom. Sorry for the delay replying. My experiences with the Observer
pattern is it works the way you expect it to. I’ve used in in Java,
PHP, and Delphi. I haven’t tried it in Ruby or Rails yet so Some folks
may have to step in here and filling gaps. But Here’s the way it should
work with some sudo-code. The following is NOT valid syntax:

class Vehicle < ActiveRecord
@SaveObservable = SaveObservable.new
def save
# do some stuff…
@SaveObservable.notify
end
def addObserver(Observer)
@SaveObservable.addObserver(Observable)
end
end

I’m pretty new to Ruby and RoR at the moment so I’m sure I did a
righteous job butchering the syntax. Sorry guys. :o)

In any case this is pretty typical to the implimentation in other
languages. I’m looking into this right now because it would be a good
idea to know the “Ruby Way” of doing Observer pattern. When I have a
better answer, I’ll write back.


#9

Tom M. wrote:

Thanks for the tip, Cody!

It sounds as though you have first hand experience of
observers in Rails. Do you know that they can work in
the ways that I described in my first post?


– Tom M.

After looking at the API documentation, there are a couple of things
that seem important to get an observer working.

  • You need to make sure your naming conventions are straight for the
    observed class and also the method names. The name of the observer must
    match the name of the class observed and the method must be one of the
    methods defined in the callback documentation.

From your original posting it looks like the method is named correctly.
Can you post a snippet of the class definition?

The other important point is telling active_record about your observer.
The following is a clip from the documentation showing what to put in
environment.rb:

config.active_record.observers = :comment_observer, :signup_observer

Notice it doesn’t have brackets around it. Maybe that has something to
do with why Rick had problems getting it to work.


#10

Tom M. wrote:

Thanks for the tip, Cody!

It sounds as though you have first hand experience of
observers in Rails. Do you know that they can work in
the ways that I described in my first post?


– Tom M.

Hi again Tom. Here’s another link that looks useful:

http://api.rubyonrails.com/classes/ActiveRecord/Observer.html