0

我在 ubuntu 12.04 上运行 rails。当我运行下面的代码时,我的一项自定义任务出现错误。问题是我不希望我的自定义任务首先运行。只是数据库迁移。

rake db:migrate --trace

rake aborted!
cannot load such file -- CSV
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `require'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `block in require'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `require'
/var/www/myapp/lib/tasks/import_inventory.rake:1:in `<top (required)>'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in `load'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in `block in load'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in `load'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/engine.rb:425:in `block in load_tasks'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/engine.rb:425:in `each'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/engine.rb:425:in `load_tasks'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application.rb:145:in `load_tasks'
/home/js/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/railtie/configurable.rb:30:in `method_missing'
/var/www/myapp/Rakefile:7:in `<top (required)>'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load_rakefile'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:501:in `raw_load_rakefile'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:82:in `block in load_rakefile'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:81:in `load_rakefile'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:65:in `block in run'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `load'
/home/js/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `<main>'
/home/js/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `eval'
/home/js/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `<main>'

正在运行的 Rake 任务:

require 'CSV'

namespace :db do
  desc 'Prepare transition database.'
  task :import_inventory => :environment do
    desc 'Import Assets Table'

    file_path = "/Users/js/data/TSI/inventory.csv"

    CSV.foreach(file_path, :headers => true) do |row|
      asset = Asset.new
      asset.asid_tag = row[0]
      asset.description = row[1]
      asset.qty = row[2]
      asset.manufactuer = row[3]
      asset.model = row[4]
      asset.save
    end
   end
end
4

1 回答 1

3

当您运行 Rake 时,所有任务文件 (lib/tasks/*.rb) 都会在 rake 确定哪些任务可用之前被加载和解析。这意味着,无论您是否调用导入任务,import_inventory.rakeruby 都会在您请求的实际任务 (db:migrate) 运行之前解析文件。结果require 'CSV'被执行,无论您真正尝试运行什么任务。

至于修复,我猜它应该是require 'csv'(小写)而不是require 'CSV'(大写)?

于 2012-08-14T17:17:45.753 回答