Question

What is the correct way in Rails w CarrierWave using Fog to delete/destroy records when there are missing attachments/images?

I am trying to clean up a few records after a missing import of images to RackSpace. There are a few missing images and thumbs. When I try to delete a record I get a error

Fog::Storage::Rackspace::NotFound

Is there a CarrierWave or Fog setting to make it more tolerant to these kinds of scenarios?

Was it helpful?

Solution

I just ran into this issue and found the original issue filed here: https://github.com/jnicklas/carrierwave/issues/481 and the wiki page describing the fix here: https://github.com/jnicklas/carrierwave/wiki/How-To%3A-Silently-ignore-missing-files-on-destroy-or-overwrite

However I wasn't happy with the solution, I didn't want to have to add those 2 methods into all of my models that use an uploader. I tend to write 1 base uploader and subclass that for any changes to specific needs. So I dug into those methods: remove_#{column_name}! and remove_previously_stored_#{column_name} and found theme here: https://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/mount.rb#L204 and https://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/mount.rb#L204

Both of these methods just call remove! on the uploader. So the easiest way to fix the issue is to override the remove! method in the uploader. Then you only need to override one method and in 1 place. My overrride looks like the following:

class CloudfilesUploader < CarrierWave::Uploader::Base
  # Override to silently ignore trying to remove missing previous file
  def remove!
    begin
      super
    rescue Fog::Storage::Rackspace::NotFound
    end
  end
end

That should solve your problems when trying to re-upload an image and overwrite an image that doesn't exist or when you just try and delete an image that doesn't exist.

~ Tom

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top