Rails 观察者有时不会开火

发布于 2024-11-09 13:52:32 字数 512 浏览 2 评论 0原文

我有一个 Rails 照片观察器模型,在删除照片后运行。如果相册中没有更多照片,则相册将取消发布。这在大多数情况下都有效,但有时不会触发,导致发布的相册中没有照片。我尝试过重现该问题,但没有成功。我的观察者方法如下:

def after_destroy(photo)
  album_photos = photo.photo_album.photos.published
  if album_photos.count > 0
    ...
  else
    photo.photo_album.update_attribute(:published, false)
  end
  expire_cache_for(photo)
end

仅供参考 - 相册更新/保存观察者仅在将其发布状态更改为 true 时才会触发,因此那里不会发生任何事情。我曾多次尝试复制这一点,但都无济于事。也没有关于此删除的 500 错误。请注意,我使用的是 Passenger,我的照片存储在 Amazon S3 上。关于如何继续调试这个的任何想法?

I have a Rails photo observer model that runs after a photo is deleted. If there are no more photos left in the album, the album is unpublished. This works most of the time, but occasionally does not fire, leaving a published album with no photos in it. I have tried replicating the problem, but to no avail. My observer method is as follows:

def after_destroy(photo)
  album_photos = photo.photo_album.photos.published
  if album_photos.count > 0
    ...
  else
    photo.photo_album.update_attribute(:published, false)
  end
  expire_cache_for(photo)
end

FYI - the photo album update/save observer only fires if changing its published status to true, so nothing occurs there. I have tried numerous times to replicate this, but all to no avail. There are no 500 errors regarding this delete either. Please note that I am using Passenger and my photos are stored on Amazon S3. Any idea on how to proceed in debugging this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

猫弦 2024-11-16 13:52:32

尝试在 after_destroy 回调中设置调试器或一些打印语句。也许它有一些奇怪的计时问题,当调用时 .count 还不是 0,你永远不知道。

我建议您深入检查并仔细检查所有执行的内容,有时看起来一切都按顺序(并且合乎逻辑),除非您检查它。

Try setting up a debugger or some print statements in that after_destroy callback. Maybe it has some weird timing issue where .count is not yet 0 when that gets called, you never know.

I suggest you dive in and double check everything that gets executed, sometimes it looks like everything is in order (and logical) unless you check it.

笑梦风尘 2024-11-16 13:52:32

在我的生产应用程序中,我还注意到观察者有时不会触发。我一直无法复制它。嗯,有时它也会发生在开发中,但我通常确定这是因为奇怪的重新加载或延迟加载问题......

Rails 3,对吗?
你已经在你的application.rb中设置了吗?
config.active_record.observers = [:my_observer]

我正在尝试在 application_controller 顶部使用 require_dependency 'my_observer' 来看看它是否有帮助。

In my production app, I also notice observers sometimes not firing. I've been unable to duplicate it. Well, sometimes it also happens in development, but I'm usually sure it's because of strange reloading or lazy-loading issues...

Rails 3, right?
You have it set in your application.rb?
config.active_record.observers = [:my_observer]

I'm trying require_dependency 'my_observer' at the top of my application_controller also to see if it'll help.

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