我在 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