For all examples, assume the class has fancy_report
and amazing_images
# business.rb
class Business < ApplicationRecord
has_one_attached :fancy_report
has_many_attached :amazing_images
end
Uploading from CSV.generate
def write_fancy_report!
to_string_io = CSV.generate do |writer|
writer << %w(header1 header2 header3 header4)
rows.each do |row|
end
end
fancy_report.attach(
io: StringIO.new(to_string_io),
filename: "fancy-file-#{Time.now.to_i}.csv",
content_type: 'text/csv',
identify: false
)
end
Uploading from URL/URI
amazing_images.attach({
io: URI.open(some_link_to_an_image),
filename: URI.parse(some_link_to_an_image).path
})