Assuming you start your rake task from the command line, Ruby looks for the file in the current working directory that the command line is in when starting the rake task. This is why it can't find it.
No such file or directory error when loading csv file in Rails
-
06-03-2022 - |
Question
I'm trying to load data from a csv file to my rails database. this is my rake task:
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
and it's saved as lib\tasks\inport_csv_data.rake. the transfers.csv file is saved in the same folder.
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
Any idea what i'm doing wrong here? it all looks preety straight forward... thanks!
Solution
OTHER TIPS
You need to explicitly point to the file. This will make file
be /path/to/your/rails/app/lib/tasks/transfers.csv
assuming the .csv
file is in fact in the same lib/tasks
folder as your rake task.
# ...
file = File.expand_path('../transfers.csv', __FILE__)
CSV.foreach(file) do |row|
# ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow