2017-08-23 4 views
0

ここでは非常に複雑な問題があります。RubyがCSV行を保存してデータベースに割り当てるためにTempfileを作成します

を私はCSVライブラリを使用して2枚のスプレッドシートをエクスポートすることだし、1、2枚のシートの間に多くの関連にある問題は、私は特定の行の値に基づいて結果レコードの外部キーを保存する必要があるとそして、ここで次の行 からのデータの残りので、ここでは一例

1023115111  | SUMMARY | 1.5 | 2 | 2.5 

8/16/2017 23:00 |  1.3 | 1.5 | 2 | 2.5 

1023201   | SUMMARY | 1.9 | 2 | 2.1 

8/16/2017 7:10 | 1.76 | 1.9 | 2 | 2.1 

8/16/2017 15:00 | 1.76 | 1.9 | 2 | 2.1 

を保存私は要約行が存在する最初のセルを取得し、それがメートルを見つける比較する必要が問題

ですそれは、この値に等しい「半ば」であるachineは

その後、行全体をスキップして、この値に等しいmachine_idは、私がしようとします

はそれをより明確にする新しいレコードに「結果」を保存

@mid = row(0)  

@machine = Machine.where(mid: @mid)  

その後、

Result.create(line: line, min: min, exps: real, max: max, 
ideal: mean, time: time, machine_id: @machine.id )  

これは私の結果モデル

です

これは私のマシンモデル

require 'csv'  
class Machine < ActiveRecord::Base  
validates_uniqueness_of :name  
has_many :results  
def self.assign_row(row) 
a, b, c = row 
if row[2].nil? 
    puts 'na' 
else 

@mid = c[0..2] 
@name = c[5..-1] 
end 
machine = Machine.find_by(name: c) 
Machine.create(name: @name, mid: @mid) if machine.blank? 
end 

def self.import(file)  
    CSV.foreach(file.path, encoding: 'iso-8859-1:utf-8', skip_blanks: true) do |row|  
machine = Machine.assign_row(row)  
    end  
end  
end  

結果移行

class CreateResults < ActiveRecord::Migration  
def change  
create_table :results do |t|  
    t.string :min  
    t.string :max  
    t.string :time  
    t.string :exps  
    t.references :machine, index: true, foreign_key: true  
    t.string :line  
    t.string :ideal  

    t.timestamps null: false  
    end  
    end  
end  

マシンの移行

class CreateMachines < ActiveRecord::Migration  
def change  
create_table :machines do |t|  
    t.string :name  
    t.string :mid  

    t.timestamps null: false  
end  
add_index :machines, :mid  
end  
end   

マシンコントローラインポート

def import  
count = Machine.import(params[:file])  
redirect_to machines_path, notice: "file imported successfully!"  
end  
です

結果コントローラのインポート

def import  
count = Result.import params[:file]  
redirect_to results_path, notice: "file imported successfully!"  
end  

は、私は私がデータベースに値を割り当て、その後一時ファイルにCSVを救うことができるかもしれないと思ったが、スキップ条件は、それを上書きしますか?

別の解決策が私の心に入ったのはhas_many_throughですが、実際にはうまくいかないことはわかりません。

答えて

0

私はそれが完璧ではない、まだそれはここで

いくつかの重要なデータをインポートした変更結果モデルコードは

require 'csv'  
class Result < ActiveRecord::Base  
belongs_to :machine  
def self.assign_row(row)  
    line, b, time, real, min, mean, max = row  
    if row[3] =~ /SUMMARY/  
    @midr = row[2].slice(1,3)  
    @mach = Machine.where(:mid => @midr).first  
    @result = Result.create(midr: time, machine_id: @mach.id )  
    else  
    line, b, time, real, min, mean, max = row  
    @result.update(exps: real, max: max, ideal: mean, time: time )  
     end  
    end  
def self.import(file)  
CSV.foreach(file.path, encoding: 'iso-8859-1:utf-8', skip_blanks: true) do |row|  
next if row.all?(&:nil?)  
result = Result.assign_row(row)  
     end  
    end  
end  
ある

を何かを把握することができました

関連する問題