If you want a full copy of the file so that both the original record and the cloned record have their own copy of the attached file, do this:
In Rails 5.2, grab this code and put it in config/initializers/active_storage.rb
, then use this code to do a copy:
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
After Rails 5.2 (whenever a release includes this commit), then you can just do this:
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
Thanks, George, for your original answer and for your Rails contributions. :)