views:

44

answers:

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.

+1  A: 

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'
Tim
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
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_idplayer_idbelongs_to :teambelongs_to :playerPlayer modelhas_many :teams, :through => :team_playersTeam Modelhas_many :players, :through => :team_players
Tim
Thanks, I think I can figure it out now.
thargor