carrierwaveuploader / carrierwave-mongoid

Mongoid support for CarrierWave
MIT License
355 stars 165 forks source link

Using carrierwave-mongoid with fog storage results in error #168

Open vertis opened 8 years ago

vertis commented 8 years ago

I am using a combination of carrierwave-mongoid (ORM support) and fog-google for the actual storage of the images.

When uploading a new image Carrierwave tries to delete the old image (that doesn't exist).

DEBUG -- : [httplog] Sending: DELETE http://www.googleapis.com:443/storage/v1/b/<snip bucket>/o/development%2Fuser%2Flogo%2F57c64d6cf0fd5a0b4d46ffcb%2F_new_

This happens because carrierwave-mongoid has:

def #{column}_will_change!
   changed_attributes["#{column}"] = '_new_'
end

Carrierwave ends up with the 'new' when calling

# lib/carrierwave/mount.rb
def remove_previously_stored_#{column}
  _mounter(:#{column}).remove_previous(*@_previous_changes_for_#{column})
end

Specifically the before variable in def remove_previous(before=nil, after=nil) (found in lib/carrierwave/mounter.rb is not nil or blank.

I'm not sure how to fix this properly, it looks like the 'new' is there for embedded document support, otherwise I would have just sent a pull request.

jnfeinstein commented 6 years ago

I'm experiencing the same issue using GridFS. I've tracked the issue down to this line. It appears that nil, the previous value, is always being overwritten by "_new_" via ||. When I remove this it works fine. Is there a reason why it has to use replace nil with "_new_"?