4
我有一個Job
模型,它可以有很多附件。 Attachment
型號上安裝了CarrierWave上傳器。克隆記錄並將遠程文件複製到新位置?
class Job < ActiveRecord::Base
has_many :attachments
end
class Attachment < ActiveRecord::Base
mount_uploader :url, AttachmentUploader
belongs_to :job
end
作業可以克隆和克隆作業應創建新的作業和附件記錄。這部分很簡單。
然後系統需要將物理文件複製到與克隆作業相關的上傳位置。
有沒有一種簡單的方法來與CarrierWave做到這一點?該解決方案應支持本地文件系統和AWS S3。
class ClonedJob
def self.create_from(orig_job)
@job_clone = orig_job.dup
if orig_job.attachments.any?
orig_job.attachments.each do |attach|
cloned_attactment = attach.dup
# Need to physically copy files at this point. Otherwise
# this cloned_attachment will still point to the same file
# as the original attachment.
@job_clone.attachments << cloned_attachment
end
end
end
end
見http://stackoverflow.com/questions/20361702/carrierwave-creating-a-duplicate-attachment-when-duplicating-its -containing-mod也使用'remote _ * _ url' –