【发布时间】: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