# ActiveRecord::Observer problem

**URL:** <https://www.ruby-forum.com/t/activerecord-observer-problem/49662>\
**Category:** Rails\
**Created:** [December 7, 2005, 9:13am UTC](https://www.ruby-forum.com/t/activerecord-observer-problem/49662 "2005-12-07T09:13:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin.jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85f322/32.png) [@kevin.jackson](https://www.ruby-forum.com/u/kevin.jackson)\
**Post date:** [December 7, 2005, 9:13am UTC](https://www.ruby-forum.com/t/activerecord-observer-problem/49662/1 "2005-12-07T09:13:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevin.jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85f322/32.png) [@kevin.jackson](https://www.ruby-forum.com/u/kevin.jackson)\
**Post date:** [December 7, 2005, 9:50am UTC](https://www.ruby-forum.com/t/activerecord-observer-problem/49662/2 "2005-12-07T09:50:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevin.jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85f322/32.png) [@kevin.jackson](https://www.ruby-forum.com/u/kevin.jackson)\
**Post date:** [December 7, 2005, 10:18am UTC](https://www.ruby-forum.com/t/activerecord-observer-problem/49662/3 "2005-12-07T10:18:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevin.jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85f322/32.png) [@kevin.jackson](https://www.ruby-forum.com/u/kevin.jackson)\
**Post date:** [December 9, 2005, 6:39pm UTC](https://www.ruby-forum.com/t/activerecord-observer-problem/49662/4 "2005-12-09T18:39:45Z")

</div>

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://lunchboxsoftware.com)  
[http://lunchroom.lunchboxsoftware.com](http://lunchroom.lunchboxsoftware.com)  
[http://rubyi.st](http://rubyi.st)
