3

我正在尝试将数据从 csv 文件加载到我的 rails 数据库。这是我的 rake 任务:

namespace :db do
require 'csv'
  desc "load user data from csv"
  task :load_csv_data  => :environment do
    CSV.foreach("transfers.csv") do |row|
      Transfer.create(
      :transfer_id => row[0],
      :net => row[1]
      )
    end
  end
end

它保存为 lib\tasks\inport_csv_data.rake。transfer.csv 文件保存在同一文件夹中。

this is what i get when trying to run the task from the shell:
$ rake db:load_csv_data --trace
** Invoke db:load_csv_data (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:load_csv_data
rake aborted!
No such file or directory - transfers.csv
c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/csv.rb:1342:in `initialize'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/csv.rb:1342:in `open'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/csv.rb:1342:in `open'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/csv.rb:1207:in `foreach'
c:/Users/uklar/openfinance/lib/tasks/import_csv_data.rake:7:in `block (2 levels) in <top (required)>'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/task.rb:227:in `call'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/task.rb:227:in `block in execute'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/task.rb:222:in `each'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/task.rb:222:in `execute'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/task.rb:166:in `block in invoke_with_call_chain'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/task.rb:159:in `invoke_with_call_chain'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/task.rb:152:in `invoke'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/application.rb:141:in `invoke_task'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/application.rb:99:in `block (2 levels) in top_level'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/application.rb:99:in `each'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/application.rb:99:in `block in top_level'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/application.rb:108:in `run_with_threads'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/application.rb:93:in `top_level'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/application.rb:71:in `block in run'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/application.rb:158:in `standard_exception_handling'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/lib/rake/application.rb:68:in `run'
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rake-10.0.2/bin/rake:37:in `<top (required)>'
c:/RailsInstaller/Ruby1.9.3/bin/rake:19:in `load'
c:/RailsInstaller/Ruby1.9.3/bin/rake:19:in `<main>'
Tasks: TOP => db:load_csv_data

知道我在这里做错了什么吗?这一切看起来都很直接......谢谢!

4

2 回答 2

4

假设您从命令行启动 rake 任务,Ruby 在启动 rake 任务时会在命令行所在的当前工作目录中查找文件。这就是它找不到它的原因。

于 2013-02-03T20:32:20.623 回答
2

您需要明确指向该文件。这将file假设/path/to/your/rails/app/lib/tasks/transfers.csv.csv文件实际上与lib/tasks您的 rake 任务位于同一文件夹中。

# ...

file = File.expand_path('../transfers.csv', __FILE__)
CSV.foreach(file) do |row|
  # ...
于 2013-02-03T20:41:57.787 回答