I have this class and I am using active storage
class MaterialsUpload < ApplicationRecord
has_one_attached :csv_file
end
This is the attachment
#<ActiveStorage::Attached::One:0x007ff1f0be9e90
@dependent=:purge_later,
@name="csv_file",
@record=
#<MaterialsUpload:0x007ff1f0c604f0
id: 3,
success: 0,
errors_list: [],
total: 0,
created_at: Mon, 12 Feb 2018 14:43:35 UTC +00:00,
updated_at: Mon, 12 Feb 2018 14:43:35 UTC +00:00>>
Is there a way I can read the data so I can do something like this
string = materials_upload.csv_file.read
CSV.parse(csv_string, headers: true) do |row|
# do something
end
Use #download
to obtain the file’s contents:
CSV.parse(materials_upload.csv_file.download, headers: true) do |row|
# ...
end
Downloads the file associated with this blob. If no block is given, the entire file is read into memory and returned. That’ll use a lot of RAM for very large files. If a block is given, then the download is streamed and yielded in chunks.