2

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.

1
  • Don't take this personally, but I'm voting to close this question because although it was a well asked question, the solution is very particular to your problem and won't be widely applicable for others. Commented May 13, 2015 at 16:59

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

thanks for your answer. This is how it looks like just for one team but contains more: <ActiveRecord::Associations::CollectionProxy [#<Team id: 2, name: "Manchester United FC", code: "MUFC", short_name: "ManU", squad_market_value: "425,000,000 €", logo: "manchester-united.png", league_id: 5, created_at: "2015-05-13 04:53:08", updated_at: "2015-05-13 04:56:50", etc...> so how do I assign values to homeTeam_id and awayTeam_id based on homeTeamName and awayTeamName from the API call? Please refer to my association. Thanks in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.