2

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 Answer 1

0

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.

4
  • This works but duplicates the image, ideal solution in this case I've now is to use same image, only modifying the my_cover.path after save the new Model object, something like this: a = Model.new b = a.dup ` b.save` b.my_cover.path = a.my_cover.path b.save - Needs to save twice because of UUID, first save creates the new UUID and second saves after change the image_path. Mar 31, 2020 at 15:59
  • You can also try to set remote_my_cover_url for the new object b to the same image URL as object a. Mar 31, 2020 at 16:57
  • This onedidn't works for me (remote_my_cover_url), I'll use the first one for now until I can find a better option - thanks! Mar 31, 2020 at 20:47
  • try this, the name might not be the same : github.com/carrierwaveuploader/… Apr 1, 2020 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.