views:

97

answers:

1

Maybe I missed this in the upgrade documentation, but if I output "Rails.root" in the rails console I see my app's root path. But if I reference "Rails.root" in a custom Rake task, it's empty. What am I missing? Thanks in advance.

Sample:

namespace :admin do
    desc "Import some data"
    task :import => :environment do
        csv = Rails.root + "/test/data.csv"
        raise "#{csv} does not exit. Stopping task." if !File.exists?(csv)

        CSV.foreach(csv, :headers => :first_row) do |row|
            puts(row['id'])
        end
    end
end

I get an exception every time because "Rails.root" is "".

+2  A: 

Try with join method

csv = Rails.root.join('test/data.csv')

csv become a Pathname of your file.

shingara
No... that didn't work. Is my task syntax correct? i.e. "task :import => :environment"
JP
yes the syntax seems correct and can works. Try without first / I update my answer with it. Because bad PathName generate.
shingara
That worked! Thanks!!
JP