Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Record Observer not firing in console/seed

I have observers set up to award badges on model changes. It works when I'm using the view, but I doesn't seem to fire when I do something like : Photo.create(:user_id => user.id, :file => file) from the console or from the seed file.

Any idea ?

class ExplorerObserver < ActiveRecord::Observer
  observe :photo

  def after_save(photo)
    user = photo.user
    Explorer.award_achievements_for(user) unless photo.new_record?
  end

end
like image 812
rnaud Avatar asked Oct 13 '22 17:10

rnaud


2 Answers

My mistake, it was a silly issue, but for the archive, here is my answer :

If you have multiple observers, dont put multiple lines like that

config.active_record.observers = :popular_observer
config.active_record.observers = :explorer_observer

instead chain your observers, my previous code was overwriting the observers with the last one !

config.active_record.observers = :popular_observer, :explorer_observer
like image 126
rnaud Avatar answered Oct 18 '22 03:10

rnaud


Did you forget to put it in config/application.rb, inside your Application class?

config.active_record.observers = :photo_observer
like image 32
Ryan Bigg Avatar answered Oct 18 '22 02:10

Ryan Bigg