【问题标题】:I want to seed my database with a json file using postgre with ruby on rails我想使用带有 ruby​​ on rails 的 postgre 为我的数据库播种一个 json 文件
【发布时间】:2020-05-01 18:49:26
【问题描述】:

解决使用

path = Rails.root.join('app', 'assets','cards.json') 作为要读取的文件的路径。

原始问题

我有一个 Json 文件,我想使用该数据在我的数据库中创建 Ruby 对象。

我尝试在我的 seed.rb 中使用它,但它不会打开 json。 有没有更好的方法来解决我想要做的事情?

#seeds.rb

require 'json'

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

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

运行 rails db:seed

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

【问题讨论】:

    标签: ruby-on-rails json ruby postgresql


    【解决方案1】:

    您需要提供正确的文件目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      相关资源
      最近更新 更多