The JSON data looks like this...
{
"data": [
"scores": {
"localteam_score": 3,
"visitorteam_score": 1,
}
"localTeam": {
"data": {
"name": "Atlanta United",
"logo_path": "https://cdn.sportmonks.com/images/soccer/teams/3/323.png",
}
},
"visitorTeam": {
"data": {
"name": "Sporting KC",
"logo_path": "https://cdn.sportmonks.com/images/soccer/teams/3/323.png",
}
},
"lineup": {
"data": [
{
"team_id": 3645,
"fixture_id": 18449920,
"player_id": 393,
"player_name": "Brad Guzan",
"number": 1,
"position": "G",}
For the table where I keep basic match information, I seeded it with this...
require 'rest-client'
rest_client = RestClient.get 'https://soccer.sportmonks.com/api/v2.0/fixtures/between/2022-01-01/2022-12-01/3645?api_token=__&include=localTeam,visitorTeam,goals,'
@match = JSON.parse(rest_client)
@match['data'].each do |match|
Match.create(
local_team: match["localTeam"]["data"]["name"],
away_team: match["visitorTeam"]["data"]["name"],
localteam_score: match["scores"]["localteam_score"],
awayteam_score: match["scores"]["visitorteam_score"],
awaylogo: match["visitorTeam"]["data"]["logo_path"],
locallogo: match["localTeam"]["data"]["logo_path"])
end
Which worked just fine and did what I wanted it to do. I needed to fill my other table, consisting of lineup data, which has been returning me "undefined method [] for nil:Nilclass" and "TypeError: no implicit conversion of string into integer" errors. So, to test, I deleted the 2nd method and kept the first, and now the first one will not even work anymore and is giving me "NoMethodError: undefined method `fetch_value' for nil:NilClass" despite being the exact same code that was working before.
I have gone into postresql and confirmed that both tables exist and that the first one has been filled properly, but now, it's like both of those methods are busted.
Full seeds file
require 'json'
require 'dotenv'
require 'rest-client'
require 'open-uri'
Dotenv.load
require 'rest-client'
rest_client = RestClient.get 'https://soccer.sportmonks.com/api/v2.0/fixtures/between/2022-01-01/2022-12-01/3645?api_token=p5o0nolOT4JUo5lYXrBFY8Ess6vuRZJ9Rt5jmOx3DmJjgZpT9ghmfYbZHIdj&include=localTeam,visitorTeam,goals,'
@match = JSON.parse(rest_client)
@match['data'].each do |match|
Match.create(
local_team: match["localTeam"]["data"]["name"],
away_team: match["visitorTeam"]["data"]["name"],
localteam_score: match["scores"]["localteam_score"],
awayteam_score: match["scores"]["visitorteam_score"],
awaylogo: match["visitorTeam"]["data"]["logo_path"],
locallogo: match["localTeam"]["data"]["logo_path"])
end
@match['data'].each do |lineup|
Lineup.create(
display_name: lineup["lineup"]["data"]["player_name"],
number: lineup["lineup"]["data"]["number"],
position: lineup["lineup"]["data"]["position"],
)
end
Full error message
$ rails db:seed
rails aborted!
NoMethodError: undefined method `fetch_value' for nil:NilClass
/home/ubuntu/environment/rated_app/db/seeds.rb:20:in `block in <main>'
/home/ubuntu/environment/rated_app/db/seeds.rb:19:in `each'
/home/ubuntu/environment/rated_app/db/seeds.rb:19:in `<main>'
/home/ubuntu/environment/rated_app/bin/rails:5:in `<top (required)>'
/home/ubuntu/environment/rated_app/bin/spring:10:in `require'
/home/ubuntu/environment/rated_app/bin/spring:10:in `block in <top (required)>'
/home/ubuntu/environment/rated_app/bin/spring:7:in `tap'
/home/ubuntu/environment/rated_app/bin/spring:7:in `<top (required)>'
Caused by:
NoMethodError: undefined method `fetch_value' for nil:NilClass
/home/ubuntu/environment/rated_app/db/seeds.rb:20:in `block in <main>'
/home/ubuntu/environment/rated_app/db/seeds.rb:19:in `each'
/home/ubuntu/environment/rated_app/db/seeds.rb:19:in `<main>'
/home/ubuntu/environment/rated_app/bin/rails:5:in `<top (required)>'
/home/ubuntu/environment/rated_app/bin/spring:10:in `require'
/home/ubuntu/environment/rated_app/bin/spring:10:in `block in <top (required)>'
/home/ubuntu/environment/rated_app/bin/spring:7:in `tap'
/home/ubuntu/environment/rated_app/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
Where am I going wrong?