我正在尝试将数据从csv文件加载到我的rails数据库。 这是我的佣金任务:
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。 transfers.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
知道我在这里做错了吗?这一切看起来都很直接...... 谢谢!
答案 0 :(得分:4)
假设您从命令行启动rake任务,Ruby会在启动rake任务时查找命令行所在的当前工作目录中的文件。这就是为什么找不到它。
答案 1 :(得分:2)
您需要明确指向该文件。这将使file
为/path/to/your/rails/app/lib/tasks/transfers.csv
,假设.csv
文件实际上与您的佣金任务位于同一lib/tasks
文件夹中。
# ...
file = File.expand_path('../transfers.csv', __FILE__)
CSV.foreach(file) do |row|
# ...