Chlorr
Chlorr

Reputation: 3

I want to seed my database with a json file using postgre with ruby on rails

SOLVED USING

path = Rails.root.join('app', 'assets','cards.json') as the path for the file to read.

ORIGINAL QUESTION

I have a Json file and I want to use the data to create Ruby objects in my database.

I tried to use this in my seed.rb but it won't open the json. Is there a better approach to what I am trying to do?

#seeds.rb

require 'json'

cards = JSON.parse(File.read('cards.json'))

cards["cards"].each do |card| 
    Card.create(card)
end

running rails db:seed

Errno::ENOENT: No such file or directory @ rb_sysopen - cards.json

Upvotes: 0

Views: 543

Answers (1)

Bùi Nhật Duy
Bùi Nhật Duy

Reputation: 322

You need to give correct directory of your file.

Upvotes: 1

Related Questions