I'm trying to find the best way to model a game in relation to teams.
The end goal is to be able to call things like:
@game.winner
@game.loser
@team.games
The first two relations are working, but the games one is not. Using has_many (see below), I get ERROR: column games.team_id does not exist
which I would normally work around by using whatever the equivalent to :foreign_key => winner_id
, but how can I have it checkout both winner_id
and loser_id
?
Is the only option to create a method in the teams model like so:
def games
won = Game.where(:winner => id)
lost = Game.where(:loser => id)
won + lost
end
So far what I'm doing is:
class Game < ActiveRecord::Base
has_one :winner, class_name: "Team"
has_one :loser, class_name: "Team"
end
class Team
has_many :games
# or something that actually works
end