2015-04-03 72 views
0
require 'csv' 
    load 'dbconnection.rb' 
    require 'activerecord' 

    class Definition<ActiveRecord::Base 

    end 
    csv_definition = File.read('C:/definition.csv') 
    spy_definition = CSV.parse(csv_definition, :headers => false) 
    spy_definition.each do |row| 
      Definition.create!(

          :id => row[id], 
          :category => row[category], 
          :name => row[name], 
          :dangerlevel => [dangerlevel], 
          :description => [description] 
          )  

        end 

我正在一個錯誤,如下所示:未定義的局部變量或方法'分類」主:對象(NameError)

datainsertion.rb:26: warning: Object#id will be deprecated; use Object#object_id 
datainsertion.rb:27: undefined local variable or method `category' for main:Object (NameError) 
    from datainsertion.rb:24:in `each' 
    from datainsertion.rb:24 
>Exit code: 1 

我需要加載該csv文件到數據庫,但我得到這個錯誤..,幫助我們解決這個問題。,

回答

0

需要 'CSV' 負載 'dbconnection.rb' 需要的ActiveRecord'

class Definition<ActiveRecord::Base 
end 
CSV.foreach("C:/definition.csv") do |row| 
Definition.create!(
    :category =>row[0], 
    :name => row[1], 
    :dangerlevel => row[2], 
    :description => row[3] 
    ) 
end 
0

錯誤真的是你所需要的。您有一個尚未定義的變量類別。嘗試'類別'。

+0

我檢查它也但它上升的誤差作爲 '[]':不能轉換字符串into Integer(TypeError) – 2015-04-03 10:56:55

+0

更多建議@Aristata – 2015-04-04 04:41:33

相關問題