ActiveRecord 观察者不会在指定的回调中调用 (Rails 3)
我的 Rails 观察者遇到了一个非常烦人的问题。 其中有些可以正常工作,有些则不能。
例如,我的 FollowObserver 是这样实现的:
class FollowObserver < ActiveRecord::Observer
def after_create(follow)
debugger
PendingMail.create({
:method => "cause_being_followed",
:data => Marshal.dump({
:follower_id => follow.user_id,
:cause_id => follow.cause_id,
})
})
end
end
它不在 Rails 服务器中调用,但在 Rails 控制台中调用(我不明白为什么)。 当我运行测试时,它按预期工作(就像运行 Rails 服务器时有些东西避免它运行一样)。
我还配置了 application.rb:
config.active_record.observers = [
:cause_observer,
:charity_follow_observer,
:comment_observer,
:follow_observer,
:news_observer
]
知道我缺少什么吗?
I'm having a very annoying problem with my rails observers.
There are some of them that work properly and some of them that don't.
For example, I have the FollowObserver implemented like this:
class FollowObserver < ActiveRecord::Observer
def after_create(follow)
debugger
PendingMail.create({
:method => "cause_being_followed",
:data => Marshal.dump({
:follower_id => follow.user_id,
:cause_id => follow.cause_id,
})
})
end
end
It's not called in the rails server but it is in the rails console (I don't get it why).
When I run the tests, it works as expected (it's like when running the rails server something is avoiding it to run).
I configured the application.rb as well:
config.active_record.observers = [
:cause_observer,
:charity_follow_observer,
:comment_observer,
:follow_observer,
:news_observer
]
Any idea what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为它应该始终被触发。我会尝试将
debugger
行替换为,然后您将能够在日志中查看操作是否正确触发。
另一个可能的原因是,虽然您认为该对象已创建,但它可能由于某种原因无效并且实际上并未创建。这可能吗?
i think it should always be triggered. I would try to replace the line
debugger
withthen you will be able to see in your logs whether the action is triggered correctly.
Another possible cause is that while you think the object is created, it might for some reason be invalid and is actually not created. Is that possible?