I'm importing objects in my seeds file from a json file like this:
[
{
"email":"[email protected]",
"id":1,
"league_id":1,
"name":"Slim Jims are good for the heart...OH YEAH!",
"owner":"Jon"
},
{
"email":"[email protected]",
"id":2,
"league_id":1,
"name":"The Bucket List",
"owner":"Robert"
}
]
my seeds file is:
require 'json'
Team.delete_all
JSON.parse(open("#{Rails.root}/doc/teams.json").read).each do |stuff|
Team.create(stuff)
end
My problem is that it assigns some random id instead of the id from the json file.
For anyone wondering, you can do this by using this syntax instead of create:
JSON.parse(open("#{Rails.root}/doc/teams.json").read).each do |stuff|
team = Team.new(stuff)
team.id = stuff['id']
team.save!
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With