ActiveRecord::Observer problem


#1

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


#2

Kev J. wrote:

@al.save
end
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 WEBrick

Kev


#3

Kev J. wrote:

bind_params(model)
@al.object_type = model.class.name
in my local_environment.rb

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 WEBrick

Solved, for anyone else out there struggling…
class ApplicationController < ActionController::Base
observer :activity_observer

end


#4

On Dec 7, 2005, at 4:19 AM, Kev J. wrote:

Solved, for anyone else out there struggling…
class ApplicationController < ActionController::Base
observer :activity_observer

end

I’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 B.
Lunchbox Software
http://lunchboxsoftware.com
http://lunchroom.lunchboxsoftware.com
http://rubyi.st