gpt4 book ai didi

ruby-on-rails - 在 Rails 6 中读取存储在 Active Storage 中的 CSV 文件

转载 作者:行者123 更新时间:2023-12-05 06:16:31 26 4
gpt4 key购买 nike

我正在使用 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 中,我找不到打开文件并只读取第一行的方法。有谁知道这样做的方法吗?谢谢!

最佳答案

怎么样?

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

关于ruby-on-rails - 在 Rails 6 中读取存储在 Active Storage 中的 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62076872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com