Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carrierwave, creating a duplicate attachment when duplicating its containing model

I would like to duplicate a model. The original model contains an attachment through Carrierwave. Ideally, a new attachment would be created, that is a copy of the original image, for the new model object.

I have looked through the Carrierwave documentation, and googled this problem, but have not found a solution that creates a new duplicate of the original image. Is this reasonable? Possible?

like image 932
eraticus Avatar asked Dec 03 '13 21:12

eraticus


1 Answers

I don't believe Carrierwave has this option. However, you can make use of the *_remote_url= method to set the new model's picture to be a duplicate of the first.

Here's a brief example

Say I have a model which has_one :photo attached with carrierwave. I can duplicate, the model, set the photo to the previous one and save it. Example:

first_model = User.first duplicate_model = first_model.dup #(where the dup code duplicates everything else you need) duplicate_model.remote_photo_url = first_model.photo_url duplicate_model.save 

This would then "copy" the photo from the first object into your second as a new carrierwave attachment.

like image 192
derekyau Avatar answered Oct 29 '22 19:10

derekyau