Rafiki Assumani
Rafiki Assumani

Reputation: 41

Populating rails database with external api request data

I'm trying to populate my database table with data from an api. I have three models involved and set up as shown below:

class Fixture < ActiveRecord::Base

 belongs_to :homeTeam, class_name: "Team", foreign_key: "homeTeam_id"
 belongs_to :awayTeam, class_name: "Team", foreign_key: "awayTeam_id"
 belongs_to :league
 validates :matchday, presence: true
 validate :opposing_teams_must_be_different

#validates :externalFixtureID, :date, :matchday, :awayTeam, :homeTeam ,   :goalsHomeTeam, :goalsAwayTeam, presence: true

 def teams   
   [homeTeam, awayTeam]
 end 

  def opposing_teams_must_be_different
     errors.add(:awayTeam, "must be different from Home team") if    awayTeam_id == homeTeam_id
  end  

 class Team < ActiveRecord::Base
    belongs_to :league
    has_many :fixtures
  end 

   class League < ActiveRecord::Base
    has_many :teams
    has_many :fixtures
  end

To populate the database and making sure I get fixtures for the the league and teams, I used the following code in the Fixture model:

    def self.query_fixtures
     uri = URI.parse("http://api.football-data.org")
     http = Net::HTTP.new(uri.host, uri.port)
     request = Net::HTTP::Get.new("/alpha/soccerseasons/354/fixtures")
     response = http.request(request)
     parsed_matches = JSON.parse(response.body)["fixtures"]
     parsed_matches.each do |key, val|

     @league = League.find(5)
     @teams = @league.teams.each do |t|
         if key["homeTeamName"] = t.name
          homeTeam_id = t.id
       elsif key["awayTeamName"] = t.name
          awayTeam_id = t.id
       end
        @fixture = @league.fixtures.create!(:gameDate => key["date"], :matchday => key["matchday"], :awayTeam_id => awayTeam_id,
                                           :homeTeam_id => homeTeam_id, :goalsHomeTeam => key["result"]["goalsHomeTeam"],
                                           :goalsAwayTeam => key["result"]["goalsAwayTeam"])
         end

      end

    end

However, when I run the code in the rails console, the database gets populated but the awayTeam_id remains blank/null. How can I make sure I get both the awayTeam_id and homeTeam_id populated? The names of the teams and what I'm getting from the api are the same. I'm setting up the awayTeam_id and homeTeam_id based on homeTeamName = team.name and awayTeamName = team.name (Please see loop in query_fixtures method) Any help would be appreciated.

Upvotes: 2

Views: 1288

Answers (2)

DiegoSalazar
DiegoSalazar

Reputation: 13541

You're using the wrong flow control logic to populate those fields, this part:

if key["homeTeamName"] = t.name
   homeTeam_id = t.id
elsif key["awayTeamName"] = t.name
   awayTeam_id = t.id
end

Is using the = assignment operator which will set the value of t.name to key["homeTeamName"] and be truthy if t.name is not nil or false meaning only homeTeam_id = t.id will ever run.

Beyond that, even if you changed the operator to the correct == equality operator, the if...elsif control flow will mean that only either homeTeam_id or awayTeam_id will get set, never both.

What does each @league.teams object look like? Is the t.id the homeTeam_id or the awayTeam_id? Is there a t.away_team_id? Add a puts t.inspect right inside your @league.teams.each block and post the result, and I'll update my answer with more specific help.

Upvotes: 1

smathy
smathy

Reputation: 27971

if key["homeTeamName"] = t.name will always be true because you've used the assignment operator (=) instead of the boolean comparison operator (==), and so the elsif will never be attempted (but would also always be true for the same reason).

Upvotes: 1

Related Questions