4
我需要創建一個循環任務,從外部文本文件創建(或編輯)產品記錄。從irb內部:如何在Rails Rake任務中使用File.open?
>> f = File.open(<filename>) # file in same directory path
沒有問題。
但是,當粘貼到Rake任務文件時,腳本總是炸彈「文件未找到」。 (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
建議?
要麼給它一個完整路徑,要麼使用['Rails.root.join'](http://apidock.com/rails/Rails/root/class),如果該文件位於Rails根目錄的某處。 –
這個伎倆。感謝喬丹! –
你爲什麼不讓這個答案? – Maarten