ActiveRecord 观察者不会在指定的回调中调用 (Rails 3)

发布于 2024-10-06 23:45:59 字数 732 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光礼记 2024-10-13 23:45:59

我认为它应该始终被触发。我会尝试将 debugger 行替换为,

logger.debug "FollowObserver::after_create"

然后您将能够在日志中查看操作是否正确触发。
另一个可能的原因是,虽然您认为该对象已创建,但它可能由于某种原因无效并且实际上并未创建。这可能吗?

i think it should always be triggered. I would try to replace the line debugger with

logger.debug "FollowObserver::after_create"

then 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文