Rails observers not firing...sometimes
I have a Rails photo obse开发者_JS百科rver 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?
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.
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.
精彩评论