Save attachment without triggering callbacks?

Hi! I am working on a CMS and when the user updates the theme, the cache (I am doing full page caching for speed) for their site is expired, and a job is enqueued to take a screenshot of the website as it looks after the change. The take-screenshot background job sets a taking_screenshot flag on the model and checks that it is set to false before actually taking a screenshot, so to avoid taking multiple screenshots of the same site at the same time. So I have an #expire_cache method that when something about the site changes, enqueues a background job that does the cache invalidation in Redis. One the cache is invalidated, this job sets the taking_screenshot flat to true and enqueues another job that actually takes the screenshot, and then sets the flag back to false. The problem is that when I set the image using Shrine, and save, the callback for the cache invalidation is called again because the site model has changed, so the cache is invalidated and the screenshot taken in an endless loop.

Is it possible to set the image in a model with Shrine, without triggering non-Shrine-related callbacks?

Thanks a lot in advance for any help.

I would recommend triggering cache invalidation at the controller level instead of the model level. Active Record callbacks should not be used for these kind of things.

Hi @janko, you’re right - that’s a much better approach so I’ve made the change. I was using callbacks to reduce some duplication but following your suggestion solved the issue I was having. Thanks!