【问题标题】:undefined local variable or method `category' for main:Object (NameError)main:Object (NameError) 的未定义局部变量或方法“类别”
【发布时间】:2015-06-08 11:02:30
【问题描述】:
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 文件加载到数据库中,但出现此错误..,请帮我解决这个问题。,

【问题讨论】:

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


    【解决方案1】:

    需要'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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我也检查了它,但它正在上升一个错误为 `[]': can't convert String into Integer (TypeError)
      • 任何更多建议@Aristata
      猜你喜欢
      • 2016-02-14
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多