Rails 使用活动存储读取 csv 文件数据

2024-02-25

我有这门课,并且正在使用主动存储

class MaterialsUpload < ApplicationRecord
  has_one_attached :csv_file
end

这是附件

#<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>>

有没有办法可以读取数据,这样我就可以做这样的事情

string = materials_upload.csv_file.read
CSV.parse(csv_string, headers: true) do |row|
    # do something
end

Use download http://edgeapi.rubyonrails.org/classes/ActiveStorage/Blob.html#method-i-download获取文件的内容:

CSV.parse(materials_upload.csv_file.download, headers: true) do |row|
  # ...
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails 使用活动存储读取 csv 文件数据 的相关文章

随机推荐