2

我有一个托管在 Heroku 上的 Rails 3.2 应用程序。我想尽量避免手动输入每条新记录的所有数据。他们是让我的 Rails 应用程序查看这样的 JSON 文件(除了 MUCH LARGER)并将该信息解析到数据库中的简单方法吗?

[   {
    "name": "Sierra Mist – Small Cup",
    "wwpplus": "5",
    "ssize": "451g",
    "calories": "190",
    "tfat": "0",
    "protein": "0",
    "fiber": "0",
    "carbs": "50",
    "restaurant_id": "12"
},
{
    "name": "Vanilla Shake regular",
    "wwpplus": "13",
    "ssize": "425g",
    "calories": "480",
    "tfat": "15",
    "protein": "14",
    "fiber": "0",
    "carbs": "74",
    "restaurant_id": "12"
},
{
    "name": "Vanilla Shake small",
    "wwpplus": "11",
    "ssize": "340g",
    "calories": "380",
    "tfat": "12",
    "protein": "11",
    "fiber": "0",
    "carbs": "60",
    "restaurant_id": "12"
}]
4

1 回答 1

11

你可以在你的db/seeds.rb文件中解析它:

records = JSON.parse(File.read('path/to/file.json'))
records.each do |record|
  ModelName.create!(record)
end

然后heroku run rake db:seed

于 2012-05-29T21:19:19.053 回答