Hi all, I have a problem with an observer. I have a simple observer... class ActivityObserver < ActiveRecord::Observer observe Customer def after_create(model) bind_params(model) @al.action = "create" @al.save logger "#{model.id} : created" end private def bind_params(model) @al = ActivityLog.new @al.user_id = User.current.id @al.customer_id = User.current.customer_id @al.object_type = model.class.name @al.object_id = model.id end end and class ActivityLog < ActiveRecord::Base set_table_name "activity_log" end in my local_environment.rb require ''lib/activity_observer'' ActivityObserver.instance why does nothing ever seem to happen when I create a new Customer? I expect the database to be populated and the log to be updated, but neither happens Looking at the WhitespaceKiller and the api docs, I don''t t hink I''m doing anything wrong, but help would be greatly appreciated Kev
Kev Jackson wrote:> Hi all, > > I have a problem with an observer. I have a simple observer... > > class ActivityObserver < ActiveRecord::Observer > observe Customer > > def after_create(model) > bind_params(model) > @al.action = "create" > @al.save > logger "#{model.id} : created" > end > > private > def bind_params(model) > @al = ActivityLog.new > @al.user_id = User.current.id > @al.customer_id = User.current.customer_id > @al.object_type = model.class.name > @al.object_id = model.id > end > end > > and > > class ActivityLog < ActiveRecord::Base > set_table_name "activity_log" > end > > in my local_environment.rb > > require ''lib/activity_observer'' > ActivityObserver.instance > > why does nothing ever seem to happen when I create a new Customer? I > expect the database to be populated and the log to be updated, but > neither happens > > Looking at the WhitespaceKiller and the api docs, I don''t t hink I''m > doing anything wrong, but help would be greatly appreciatedJust some more info on this, if I run script/console, I can create Customers from the console and the observer fires and stores the data correctly - so it''s only not doing anything within WEBrick Kev
Kev Jackson wrote:> Kev Jackson wrote: > >> Hi all, >> >> I have a problem with an observer. I have a simple observer... >> >> class ActivityObserver < ActiveRecord::Observer >> observe Customer >> >> def after_create(model) >> bind_params(model) >> @al.action = "create" >> @al.save >> logger "#{model.id} : created" >> end >> >> private >> def bind_params(model) >> @al = ActivityLog.new >> @al.user_id = User.current.id >> @al.customer_id = User.current.customer_id >> @al.object_type = model.class.name >> @al.object_id = model.id >> end >> end >> >> and >> >> class ActivityLog < ActiveRecord::Base >> set_table_name "activity_log" >> end >> >> in my local_environment.rb >> >> require ''lib/activity_observer'' >> ActivityObserver.instance >> >> why does nothing ever seem to happen when I create a new Customer? I >> expect the database to be populated and the log to be updated, but >> neither happens >> >> Looking at the WhitespaceKiller and the api docs, I don''t t hink I''m >> doing anything wrong, but help would be greatly appreciated > > > Just some more info on this, if I run script/console, I can create > Customers from the console and the observer fires and stores the data > correctly - so it''s only not doing anything within WEBrickSolved, for anyone else out there struggling... class ApplicationController < ActionController::Base observer :activity_observer ... end
On Dec 7, 2005, at 4:19 AM, Kev Jackson wrote:> > Solved, for anyone else out there struggling... > class ApplicationController < ActionController::Base > observer :activity_observer > ... > > endI''m a little late (trying to keep up with this list is growing difficult), but yes. Unfortunately, when observers are loaded through ActiveRecord::Base.observers (which is how config.active_record.observers works), the observers do not get set up with the dependencies system and do not reload in development mode. It''s rather a pain but the work around is, as you''ve found, to set them on a controller such that they''ll be reloaded along with the controller class. Just throwing some "whys" at your answer. -- Scott Barron Lunchbox Software http://lunchboxsoftware.com http://lunchroom.lunchboxsoftware.com http://rubyi.st
Seemingly Similar Threads
- simple SQLITE3 error!!??library routine called out of sequence
- Decoupled observers for controllers?
- Trouble installing SWAT on a Samba 4 Alpha 13 build on Ubuntu Server
- Trouble installing SWAT on a Samba 4 Alpha 13 build onUbuntu Server
- Trouble installing SWAT on a Samba 4 Alpha 13 buildonUbuntu Server