未定义的局部变量或方法`category' for main:Object(NameError)

时间:2015-04-03 10:50:51

标签: mysql ruby-on-rails ruby csv rubygems

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

我收到如下错误:

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文件加载到数据库中,但我收到此错误..,帮助我解决这个问题。,

2 个答案:

答案 0 :(得分:0)

错误确实是你所需要的。您有一个尚未定义的变量类别。试试'类别'。

答案 1 :(得分:0)

要求&#39; csv&#39;     load&#39; dbconnection.rb&#39;     要求&#39; activerecord&#39;

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