【发布时间】:2023-04-09 03:01:01
【问题描述】:
我有一个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
【问题讨论】:
标签: ruby-on-rails ruby amazon-s3 ruby-on-rails-3.2 carrierwave