Association problem in one direction

I have these models

class Game < ActiveRecord::Base
belongs_to :home_team, :class_name => ‘Team’
belongs_to :away_team, :class_name => ‘Team’
belongs_to :field
class Person < ActiveRecord::Base
has_many :affiliations
has_many :teams, :through => :affiliations
class Team < ActiveRecord::Base
has_many :affiliations
has_many :players, :through => :affiliations, :source => :person
has_many :games
class Affiliation < ActiveRecord::Base
belongs_to :person
belongs_to :team

I with the associations above, I can get the players for the teams,
the home team and away team players from the games.

I want to be able to go back and get the games that each team and/or
player plays in. The associations work in one direction but not in the
other. How do I work back through the home_team and away_team
connections and get home games, away games and the totals for a given
player or team?

Any ideas?



Rails cannot infer how to get from Games → Teams, from your models
rails is looking for team_id in Games

Based on your models, I can see only one way:

class Team < ActiveRecord::Base
has_many :affiliations
has_many :players, :through => :affiliations, :source
=> :person
has_many :home_games, :class => ‘Games’, :foreign_key =>
has_many :away_games, :class => ‘Games’, :foreign_key =>

You could then access team.away_games and team.home_games

On Feb 26, 3:13 pm, “[email protected][email protected]