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.instanceIt 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. :-) 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 Mornini
On 3/9/06, Tom Mornini <tmornini@infomania.com> wrote:> 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. :-) > > 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 Morniniin 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 Olson http://techno-weenie.net
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 Mornini On Mar 9, 2006, at 5:36 PM, Rick Olson wrote:> On 3/9/06, Tom Mornini <tmornini@infomania.com> wrote: >> 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. :-) >> >> 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 Mornini > > 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 Olson > http://techno-weenie.net > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Tom Mornini 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 MorniniAre 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. -- Posted via http://www.ruby-forum.com/.
Cody Skidmore 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 -- Posted via http://www.ruby-forum.com/.
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 Mornini On Mar 10, 2006, at 2:13 PM, Cody Skidmore wrote:> Cody Skidmore 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On 3/10/06, Tom Mornini <tmornini@infomania.com> 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 MorniniActiveRecord::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 Olson http://techno-weenie.net
Tom Mornini 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 MorniniHI 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. -- Posted via http://www.ruby-forum.com/.
Tom Mornini 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 MorniniHi again Tom. Here''s another link that looks useful: http://api.rubyonrails.com/classes/ActiveRecord/Observer.html -- Posted via http://www.ruby-forum.com/.
Tom Mornini 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 MorniniAfter 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. -- Posted via http://www.ruby-forum.com/.