【问题标题】:Reading a CSV file stored in Active Storage in Rails 6在 Rails 6 中读取存储在 Active Storage 中的 CSV 文件
【发布时间】:2020-05-29 00:47:18
【问题描述】:

我正在使用 Active Storage 上传 CSV 文件,然后读取这些文件以更新数据库中的值。我已经设置了一个基本的上传文件类来做到这一点;

class VendorFile < ApplicationRecord
    has_one_attached :vendor_upload_file
    validate :acceptable_file
end

我使用这样的 Rails 6 打开方法打开 CSV 文件;

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

这非常适合处理整个文件。问题是在处理之前我想打开文件并阅读第一行以确保文件格式正确。如果文件存储在 Active Storage 中,我找不到打开文件并仅读取第一行的方法。有谁知道这样做的方法? 谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby csv rails-activestorage


    【解决方案1】:

    怎么样?

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

    【讨论】:

      猜你喜欢
      • 2018-11-26
      • 1970-01-01
      • 2018-07-26
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      相关资源
      最近更新 更多