3

I'm using Active Storage to upload CSV files, which are then read to update values in the database. I have set up a basic upload file class to do this;

class VendorFile < ApplicationRecord
    has_one_attached :vendor_upload_file
    validate :acceptable_file
end

I open the CSV files using the Rails 6 open method like this;

self.vendor_upload_file.open do |file|
  CSV.foreach(file) do |row|
    do some processing....
  end
end

This works great for processing through the whole file. The issue is before processing I'd like to open the file and just read the first line to ensure the file is the correct format. I cannot find a way to open the file and read just the first line if the file is stored in Active Storage. Does anyone know a way to do this? Thanks!

1 Answer 1

2

How about?

headers = self.vendor_upload_file.open(&:first).parse_csv
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.