【问题标题】:What causes the error "undefined local variable or method `csv' for main:Object"?是什么导致错误“未定义的局部变量或方法 `csv' for main:Object”?
【发布时间】:2014-09-18 09:59:43
【问题描述】:

我正在尝试编写一个用于将 CSV 文件导入多个模型的 rake 任务。代码编译没有错误,但是当我尝试运行它时收到此错误消息:

rake 中止! NameError: undefined local variable or method csv' for main:Object /Users/rickcasey/Projects/Programming/wfrails/lib/tasks/import_partial.rake:28:in block in '

这是脚本:

desc "Imports the  CSV file "
task :import_partial => :environment do

    require 'csv'    

    csv.foreach('public/partial.csv', :headers => true) do |row|

            # create records in independent tables

            # create the Company object
            this_company_name = row.to_hash.slice(*%w[county_name])
            if !(Company.exists?(company_name: this_company_name))
              Companies.create(row.to_hash.slice(*%w[company_name operator_num]))
            end
            thecompany = Company.find(this_company_name)
            company_id = thecompany.id

            # create the County object
            this_county_name = row.to_hash.slice(*%w[county])
            if !(County.exists?(county_name: this_county_name))
              Counties.create(county_name: this_county_name)
            end
            thecounty = County.find(this_county_name)
            county_id = thecounty.id

            # create the GasType object  
            this_gastype_name = row.to_hash.slice(*%w[gas_type])
            if !(GasType.exists?(gastype_name: this_gastype_name))
              GasType.create(gastype_name: this_gastype_name)
            end
            thegastype = GasType.find(this_gastype_name)
            gastype_id = thegastype.id


            # create the Field object
            this_field_name = row.to_hash.slice(*%w[field])
            if !(Field.exists?(field_name: this_field_name))
              Field.create(field_name: this_field_name, field_code: field_code)
            end
            thefield = Field.find(this_field_name)
            field_id = thefield.id

            # create the Formations object  
            this_formation_name = row.to_hash.slice(*%w[formation])
            if !(Formation.exists?(formation_name: this_formation_name))
              Counties.create(formation: this_formation_name, formation_code: formation_code)
            end
            theformation = Formation.find(this_formation_name)
            formation_id = theformation.id

            # debugging:
            puts "company_id:", company_id
            puts "county_id:", county_id
            puts "gastype_id:", gastype_id
            puts "field_id:", field_id
            puts "formation_id:", formation_id

            # create records in dependent tables:
            # Use the record id's from above independent table create records containing foreign keys:

            #Facilities.create(row.to_hash.slice(*%w[dir_e_w dir_n_s dist_e_w dist_n_s facility_name facility_num ground_elev lat long meridian qtrqtr range sec twp utm_x utm_y])

            #Wells.create(row.to_hash.slice(*%w[api_county_code api_seq_num first_prod_date form_status_date formation_status sidetrack_num spud_date status_date td_date test_date wbmeasdepth wbtvd well_bore_status well_name])


    end
end

我的环境是:ruby 2.1.2p95,Rails 4.1.1

这很不清楚,并且还没有找到类似错误的示例,但我理解的答案......非常感谢任何帮助!

【问题讨论】:

  • 错误发生在哪一行?

标签: ruby rake


【解决方案1】:

我相信错误在这一行

csv.foreach('public/partial.csv', :headers => true) do |row|

应该是

CSV.foreach('public/partial.csv', :headers => true) do |row|

【讨论】:

    【解决方案2】:

    我相信类名是大写的 - CSV.foreach,而不是 csv.foreach

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2014-04-21
      • 2016-04-08
      • 1970-01-01
      相关资源
      最近更新 更多