如果您想要文件的完整副本,以便原始记录和克隆记录都有自己的附件副本,请执行以下操作:
在 Rails 5.2 中,获取这段代码并将其放入 中config/initializers/active_storage.rb
,然后使用这段代码进行复制:
ActiveStorage::Downloader.new(original.poster_image).download_blob_to_tempfile do |tempfile|
copy.poster_image.attach({
io: tempfile,
filename: original.poster_image.blob.filename,
content_type: original.poster_image.blob.content_type
})
end
在 Rails 5.2 之后(只要一个版本包含这个 commit),那么你可以这样做:
original.poster_image.blob.open do |tempfile|
copy.poster_image.attach({
io: tempfile,
filename: original.poster_image.blob.filename,
content_type: original.poster_image.blob.content_type
})
end
感谢 George,您的原始答案和 Rails 贡献。:)