Carrierwave,在复制其包含的模型时创建重复的附件 [英] Carrierwave, creating a duplicate attachment when duplicating its containing model
问题描述
我想复制一个模型。原始模型包含通过Carrierwave的附件。理想情况下,将为新的模型对象创建一个新的附件,即原始图像的副本。
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.
我已经浏览了Carrierwave文档,并在Google上进行了搜索问题,但尚未找到创建原始图像新副本的解决方案。这合理吗?
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?
推荐答案
我不相信Carrierwave有此选项。但是,您可以使用* _remote_url =方法将新模型的图片设置为第一张图片的副本。
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.
这里是一个简短的示例
说我有一个具有has_one的照片:附有载波的照片。我可以复制模型,将照片设置为上一张并保存。示例:
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.
这篇关于Carrierwave,在复制其包含的模型时创建重复的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!