Rails Watcher not working

I try to use observers in a rails application to create a new entry in my "Events" model every time a new "comment" is saved. Comments are saved well, but the observer does not create events properly.

// comment_observer.rb class CommentObserver < ActiveRecord::Observer observe :comment def after_save(comment) event = comment.user.events.create event.kind = "comment" event.data = { "comment_message" => "#{comment.message}" } event.save! end 

This observer works fine, I use it in the console, but it does not seem to be observed properly; when I try to use the application, it just does not generate events. I see no errors or anything else.

I also have config.active_record.observers = :comment_observer in the environment.rb file.

Where am I mistaken? Should I take a different approach?

+4
source share
2 answers

You do not need to use the observe statement, since your class is named CommentObserver.

Try to leave it.

Or try:

 observe Comment 

instead

 observe :comment 
+2
source

Indeed, you need to observe :comment only if the comment class cannot be inferred from the observer name (i.e. it is not called CommentObserver).

You declared your observer in application.rb:

 # Activate observers that should always be running config.active_record.observers = :comment_observer 
+22
source

All Articles