1

I am attempting to model a table tennis match in rails. Here is what I have:

Game Model:
team_1_score
team_2_score
team_1_id
team_2_id

Team Model:
game_id
player_id

Player Model:
Name

So each game will consist of 2 teams (of either 1 or 2 players each).
Then I was planning on linking game to player with has_many, :through. I don't think this will work because of the 2 instances of team in each game. But I really don't know where I should go from here. Any help would be greatly appreciated.

thargor
  • 453
  • 3
  • 11
  • For a `Game`, how do you decide which is `team_1` and which is `team_2`? Do you have something line `home_team` and `away_team` or any other distinguishing feature? – Swanand Oct 05 '10 at 05:42
  • Team 1 and team 2 are arbitrary and only act as a way to group players for a match. These teams can change regularly and often. – thargor Oct 05 '10 at 05:58

1 Answers1

1

I'm not sure how to do the has_many :through between players and games, but it might be easier if you start out with something like this:

Team Model
id
name
has_many :players
has_many :games

Player Model
id
name
team_id 
has_one :team

Then the Games model would have something like (in addition to what you already have):

has_one :team1, :class_name => 'Team'
has_one :team2, :class_name => 'Team'
coder_tim
  • 1,710
  • 1
  • 12
  • 13
  • I don't want a player to be assigned a team but I do want a team to be composed of players. I essentially want to be able to create a team from the players. But I will work with what you gave me and see if I can figure it out. Thanks – thargor Oct 05 '10 at 05:56
  • Well, you could introduce another table called "team_players", which just has foreign keys tying the players to the teams. This would give you the flexibility of assigning players to more than 1 team. (Sorry, I can't seem to format this right) TeamPlayer model: team_id player_id belongs_to :team belongs_to :player Player model has_many :teams, :through => :team_players Team Model has_many :players, :through => :team_players – coder_tim Oct 05 '10 at 06:10
  • Thanks, I think I can figure it out now. – thargor Oct 08 '10 at 23:12