How to use File.open inside a Rails Rake task?

I need to create a repeating task that creates (or edits) product records from an external text file. Inside irb:

>> f = File.open(<filename>) # file in same directory path 

No problems.

But when you insert a Rake task into the file, the script always bombes "File not found." (Rails 3.1, Ubuntu.)

 namespace :sap do desc "uploads data from raw SAP file" task :upload => :environment do f = File.open("sap_pnlist_20111010a.csv") records = f.readlines records.each {|row| ... etc etc ... } end end 

Suggestions?

+4
source share
1 answer

If the file is somewhere inside your Rails root, use

 Rails.root.join('grandparent_dir', 'parent_dir', 'file.txt') 

If the file does not belong to your Rails root , you must give it the full path .

+4
source

All Articles