sharataka
sharataka

Reputation: 5132

How to add external data to database using rake db:seed?

I have the following in my seeds.rb file. When I run rake db:seed, I get an error saying:

rake aborted!
No such file or directory - MSFT_1_100.json

This displays even though I have the file 'MSFT_1_100.json' in the same folder as the seeds.rb file (/project/db). Any advice on how to fix this?

require 'json'
file_name = 'MSFT_1_100.json'

data = File.open(file_name, "r").read
my_object = JSON.load(data)
my_object.each do |item|
    new_review = Review.create(:company => 'Microsoft', :pro => item['pro'], :con => item['con'], :advice => item['advice'], :role => item['role'])
end

Upvotes: 2

Views: 798

Answers (1)

yednamus
yednamus

Reputation: 583

try to do this instead of just giving the file_name directly

file_name = File.expand_path(File.join(File.dirname(__FILE__),'MSFT_1_100.json'))

Upvotes: 4

Related Questions