Rails使用活动存储读取csv文件数据 [英] Rails Read csv file data with active storage

查看:77
本文介绍了Rails使用活动存储读取csv文件数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个课程,我正在使用活动存储

I have this class and I am using active storage

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

有没有一种方法可以读取数据,所以我可以做类似的事情

Is there a way I can read the data so I can do something like this

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

推荐答案

使用

Use download to obtain the file’s contents:

CSV.parse(materials_upload.csv_file.download, headers: true) do |row|
  # ...
end

这篇关于Rails使用活动存储读取csv文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆