Active Record Observer 未在控制台/种子中触发
我设置了观察员来奖励模型变化的徽章。 当我使用视图时它可以工作,但是当我执行以下操作时我似乎不会触发: Photo.create(:user_id => user.id, :file => file) 从控制台或种子文件。
有什么想法吗?
class ExplorerObserver < ActiveRecord::Observer
observe :photo
def after_save(photo)
user = photo.user
Explorer.award_achievements_for(user) unless photo.new_record?
end
end
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的错误,这是一个愚蠢的问题,但对于存档,这是我的答案:
如果你有多个观察者,不要像这样放置多行,
而是链接你的观察者,我以前的代码是用最后一个观察者覆盖观察者!
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
instead chain your observers, my previous code was overwriting the observers with the last one !
您是否忘记将其放入
Application
类中的config/application.rb
中?Did you forget to put it in
config/application.rb
, inside yourApplication
class?