Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从多个CSV文件中的特定列加载Rails表的最佳方法?_Ruby On Rails_Postgresql_Csv - Fatal编程技术网

Ruby on rails 从多个CSV文件中的特定列加载Rails表的最佳方法?

Ruby on rails 从多个CSV文件中的特定列加载Rails表的最佳方法?,ruby-on-rails,postgresql,csv,Ruby On Rails,Postgresql,Csv,我有一个CSV数据集,有100多列,标题被分成两个文件,还有一个Rails/PostgreSQL中的表,其中的字段意味着只获取特定的列数据,大约60个特定的列 解析CSV并创建一个表条目来填充我需要的特定列的键的最佳方法是什么 作为一个示例,这就是我到目前为止使用的:ID、:DESC、:PROGRAM、:PROGRAM_DESC作为我需要从数据集中提取的一些列名,而p_ID、description、PROGRAM_name和PROGRAM_description作为表中的字段 似乎不起作用,所有

我有一个CSV数据集,有100多列,标题被分成两个文件,还有一个Rails/PostgreSQL中的表,其中的字段意味着只获取特定的列数据,大约60个特定的列

解析CSV并创建一个表条目来填充我需要的特定列的键的最佳方法是什么

作为一个示例,这就是我到目前为止使用的:ID、:DESC、:PROGRAM、:PROGRAM_DESC作为我需要从数据集中提取的一些列名,而p_ID、description、PROGRAM_name和PROGRAM_description作为表中的字段

似乎不起作用,所有条目都被nil值填充

file = File.read(Rails.root.join('lib', 'assets', 'test.csv'))
fileCSV = CSV.parse(file, :headers => true)

fileCSV.each do |row|
    Program.create_or_find_by!(p_id: row[:ID], description: row[:DESC], program_name: row[:PROGRAM], program_description: row[:PROGRAM_DESC])
end
我计划对第一个文件中的每个条目进行创建,然后查找条目并匹配它们以更新第二个文件中的必要列。但是,编写逻辑以将列与相应字段匹配的最佳方法是什么

编辑: 我想我已经成功了,至少在这4个专栏里

我看过row.to_散列,一开始没有得到逻辑,但想法是将整行保存到散列中,然后在创建条目时分配值

file = File.read(Rails.root.join('lib', 'assets', 'test.csv'))
fileCSV = CSV.parse(file, :headers => true)

fileCSV.each do |row|
    rowHash = row.to_hash
    Program.create_or_find_by!(p_id: rowHash['ID'], description: rowHash['DESC'], program_name: rowHash['PROGRAM'], program_description: rowHash['PROGRAM_DESC'])
end
当ID是rowHash中的一个符号时,我无法读取它,但当它是一个字符串时,它会读取,就像rowHash['ID']。我检查了ID值上的.class,它读取为字符串,即使字段接受整数,ID是否会正确输入


仍在寻找从多个CSV加载单个条目的有效方法…

试试这个,为我工作

## services/programs/csv.rb
require 'csv'
module Programs
    class Csv
        def self.import(file)
            new(file).import
        end
        
        def import
            CSV.foreach(file.path, col_sep: ';', headers: true, encoding: 'bom|utf-8', strip: true) do |row|
                data = row.to_h
                p = Program.where(p_id: data['ID']).first_or_initialize
                p.description = data['Desc']
                p.program_name = data['PROGRAM']
                p.program_description = data['PROGRAM_DESC']
                p.save
            end
        end
        
        def initialize(file)
           @file = file
        end

    attr_accessor :file
    end
end
并在需要的地方调用
Programs::Csv.import(file)

这个设置对我很有用,我希望它能对您有所帮助。

可能不是最好的解决方案,但我会使用pg_copy功能创建一个包含CSV中所有列的暂存表。然后使用
ActiveRecord::Base.connection.execute
可以创建一个SQL字符串,将所需的列和值向上插入到Destination表中。然后,您可以在流程完成后截断临时表。