1

I have data I need to seed my heroku app with for production. When I run the heroku run rake db:seed command I get an error, "rake aborted! no such file in directory"

I've tried creating a custom rake task and I get the same error.

The beginning of my rake task is as follows:

require 'csv'
CSV.foreach("/Users/username/Documents/Apps Folder/myapp/seed_file.txt") do |row|
   ...
end

Not sure if my issue is where I have my CSV file located, or if it's something else, I've looked at other related questions on stackoverflow and they haven't been able to fix my problem. Does anyone know how to solve this issue?

EDIT: I've added the error message I receive in terminal when I try to run my custom rake task below:

Running `rake customraketask` attached to terminal... up, run.8243
rake aborted!
No such file or directory -/Users/username/Documents/Apps Folder/myapp/app/assets/files/seed_file.txt
/app/lib/tasks/customraketask.rake:13:in `block in <top (required)>'
Tasks: TOP => customraketask
(See full trace by running task with --trace)
Reuben
  • 701
  • 1
  • 9
  • 17

3 Answers3

2

Instead of typing in your current path to the file (which looks to be the file directory structure of OSX, try doing something like

File.expand_path("../../seed_file.txt", __FILE__)

Which would give you a path relative to the current file, concatenated with the ../../seed_file.txt path and processed will return the actual absolute path. What you would probably see, provided your file is located at db/seeds.rb is: /home/username/Apps Folder/my_app/seed_file.txt.

See this previous question for more information about __FILE__

EDIT: Since you have spaces in your filepath, try the following:

require 'shellwords'
File.expand_path("../seed_file.txt", __FILE__).shellescape

EDIT #2: I think you've misunderstood my answer. I'll re-explain it here using a different method.

So, if my file directory looks like this:

├───App
|   └───db
|       ├───seeds.rb
|   ├───seed_file.txt

Then I would type this into my seeds.rb file

# seeds.rb
CSV.foreach( File.join(File.dirname(__FILE__), '../seed_file.txt') ) do |row|
    puts row
end
Community
  • 1
  • 1
Momer
  • 3,158
  • 22
  • 23
  • I used the File.expand_path which gave me the absolute path, it's identical to what I already have written so I am still getting the same error – Reuben Sep 30 '13 at 20:19
  • I added the shellwords and used the shellescape method and I still get the same error – Reuben Sep 30 '13 at 21:27
  • Then I'd say you're back to square one: There is no file at the path that File.expand_path outputs. – Momer Sep 30 '13 at 21:32
  • Yeah it's strange, in terminal I went to the folder where I'm holding my csv files and did a pwd to make sure I have an exact file path to that location, I then used that for the File.expand_path, which gave the same output, but for some reason Heroku can't see it – Reuben Sep 30 '13 at 21:38
  • This is what I did `a = File.expand_path("/Users/username/Documents/Apps Folder/myapp/app/assets/files/seed_file.txt", __FILE__).shellescape` `CSV.foreach(a) do |row|` – Reuben Sep 30 '13 at 21:55
  • The whole point here is that you should **not** be typing in `"/Users/username/"`. On heroku, that path would be something like `"/home/username"`, **not** `/Users/username`. – Momer Sep 30 '13 at 22:01
  • I see..I also tried CSV.foreach('/Users/username/Documents/Apps Folder/myapp/app/assets/files/seed_file.txt'.shellescape) and got the same error – Reuben Sep 30 '13 at 22:40
0

If you read the error message carefully, you would realize the Heroku would not have your /Users/username ... directory.

Try CSV.foreach("./seed_file.txt") instead.

Benjamin Tan Wei Hao
  • 9,621
  • 3
  • 30
  • 56
0

judging from your file path, it looks like you are trying to seed from a file that is located on your local system, not at Heroku. there should be a seeds.rb file in the db folder of your rails app where you can configure your database seeding. try adding your seed_file.txt to the rails project folder maybe app > assets > files > seed_file.txt or something similar and reference that file path instead.

celeriko
  • 1,564
  • 1
  • 13
  • 26