Observer not working

Help please.

I’m trying to observe a User class, but I can’t get this to work, the
after_create method never gets called… (the breakpoint never gets
called)

app/models/user_observer.rb:

class UserObserver < ActiveRecord::Observer
def after_create(user)
breakpoint
setting = get_setting(‘sys_notify_enable_user_create’)
if setting? then
AdminMailer.deliver_simple_monitor_alert(‘Se ha creado
un nuevusuario’,
"Nombre: #{user.name} \nCorreo: #{user.email} \nDetalle:
#{user.detail} ")
end
end
end
UserObserver.instance

config/enviroment.rb:
config.active_record.observers = :user_observer

Thanks everybody

you didn’t mention this, so i’ll ask. you changed environment.rb. so
did you make sure to restart your webserver?

Chris

Take a peek at the acts_as_authenticated plugin, you should get plenty
of inspiration from what they’ve done.

http://technoweenie.stikipad.com/plugins/show/Acts+as+Authenticated

-z

Yes… I’ve done that already… :frowning:

Chris H.
escribió:

you didn’t mention this, so i’ll ask. you changed environment.rb. so

In my observer (which actually observers several models) file, I don’t
have the UserObserver.instance line you have.

and also, since my observer observes several models, I have to tell it
which models to observe since it can’t infer from the observer name

app/models/audit_observer.rb
AuditObserver < ActiveRecord::Observer

observer Order, Box, LineItem

def after_create(model)
model.log(…)
end

def after_update(model)
model.log(…)
end

def after_destroy(model)
model.log(…)
end
end

notice no AuditObserver.instance here

then in environment.rb i have:

config.active_record.observers = :audit_observer

thats all i have and everything works as expected.

try removing the UserObserver.instance line at the end of your
observer definition and see what happens (restart webserver just to be
safe).

I don’t think specifying

observer User

in the observer class definition would do much as rails is supposed to
determine what to observe based on the class name of the observer. you
could try it though, and see what happens. wouldn’t hurt.

hope this helps.

Chris

hmmm, i don’t remember if i did that or not. my guess is that it’s
there (can’t check because code is at work), i just didn’t check my
ApplicationController when i provided my settings.

glad you got it working.

Chris

Hi,

I’ve removed UserObserver.instance and the observe User line too…
then
restarted webrick… and still not working… I’ve checked, and
rechecked
everithing and I can’t find what is wrong… finally I’ve put on
app/application.rb inside ApplicationController class the following:
observer :user_observer

and now everything is working…but I still don’t understand why this
doesn’t work as expected from enviroment.rb.

Thanks

Chris H.
escribió:

e my observer observes several models, I have to tell it

Thanks a lot for your help and your time

Chris H.
escribió:

hmmm, i don’t remember if i did that or not. my guess is that it’s