Forum: Ruby on Rails ActiveRecord::Observer problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 kevin.jackson (Guest)
on 2005-12-07 09:13
(Received via mailing list)
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
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 kevin.jackson (Guest)
on 2005-12-07 09:50
(Received via mailing list)
Kev Jackson 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
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 kevin.jackson (Guest)
on 2005-12-07 10:18
(Received via mailing list)
Kev Jackson 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
6993d6b4b6d882f421e1b45ac147f3e0?d=identicon&s=25 scott (Guest)
on 2005-12-09 18:39
(Received via mailing list)
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
> ...
>
> 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 Barron
Lunchbox Software
http://lunchboxsoftware.com
http://lunchroom.lunchboxsoftware.com
http://rubyi.st
This topic is locked and can not be replied to.