CarrierWave how to change object path after downloaded

Viewed 164

I've to duplicate a Model which contains a CarrierWave image but I need to use the same image file, the problem is that my file path it's using the model UUID and after saving new object the UUID of the image is updated too.

How can I change the image file path after saving the object to keep the original image path?

This is my image stored at aws:

@mounted_as=:my_cover, @storage=#<CarrierWave::Storage::Fog:0x0000000002ef @uploader=#<CoverUploader::Uploader82999640:0x0000000002ef ...>>, @file=#<CarrierWave::Storage::Fog::File:0x0000000002ef @uploader=#<CoverUploader::Uploader82999640:0x0000000002ef

I can access Model.my_cover.file.path or Model.my_cover.cover.path but I can't change the path value.

I don't want to duplicate the image because this will double the file storage space.

Thanks for any help!

1 Answers

When duplicating the object, you should be able to copy along the image from the previous object before saving the new one. Let's say your model is called Model and has an image attribute called my_cover.

a = Model.new
b = a.dup

b.my_cover = a.my_cover
b.save

Carrierwave uses an image object that references an image URL and that you can copy to a different object.

Related