# Basic has\_many question

**URL:** <https://www.ruby-forum.com/t/basic-has-many-question/78474>\
**Category:** Rails\
**Created:** [December 6, 2006, 1:31am UTC](https://www.ruby-forum.com/t/basic-has-many-question/78474 "2006-12-06T01:31:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 6, 2006, 1:31am UTC](https://www.ruby-forum.com/t/basic-has-many-question/78474/1 "2006-12-06T01:31:22Z")

</div>

Hi there, I’m still new at this, but I’m creating a quick little  
foosball stats app for my office(!) and I have a question about  
relationships.

I have a table called games and a table called players. I’d like it so  
when people enter information about a game they can choose from four  
dropdowns the four players that played in the game.

At first I wasn’t thinking and thought it would be a basic thing like  
this:

game.rb  
has\_many :players

and

player.rb  
belongs\_to :game

But then I couldn’t figure out how to link four players to one game.

Am I missing something really obvious? Is this a habtm instance?

Thanks!

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 6, 2006, 2:11am UTC](https://www.ruby-forum.com/t/basic-has-many-question/78474/2 "2006-12-06T02:11:22Z")

</div>

Assuming that players play in more than one game and games have more  
than one player, it is a has\_and\_belongs\_to\_many situation, yes.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 6, 2006, 6:04am UTC](https://www.ruby-forum.com/t/basic-has-many-question/78474/3 "2006-12-06T06:04:20Z")

</div>

The link that connects an instance of 4 players to a particular game  
is called a Match. Different sports have different names for it, in  
cricket it is a Test Match, in tennis it is a Match. So use the  
terminology that is used in your foosball domain.

All the relevant data for that particular Match will go into that  
table, for instance the stadium name, date of the game, whether the  
Match was sold out etc. This is in addition to the foreign keys that  
connect the other two classes.

You will have this association table with just the foreign keys if  
your app does not need any other data mentioned above.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 6, 2006, 4:43pm UTC](https://www.ruby-forum.com/t/basic-has-many-question/78474/4 "2006-12-06T16:43:48Z")

</div>

Consider a join table, using “has\_many :through” constructs. This  
allows you to record particulars of that players involvment in the game  
(team, position, etc). I believe this is what Dave was suggesting with  
the ‘match’ table, though match and game sound synonymous, and I’d use  
someting to indicate involvement, like ‘encounters’ (derived from a  
verb)

encounter.rb  
belongs\_to :player  
belongs\_to :game

player.rb  
has\_many :encounters  
has\_many :games, :through =\> encounters

game.rb  
has\_many :encounters  
has\_many :players, :through =\> encounters

See the rails docs on ‘has\_many’, and Susser’s writings on  
associations:  
[http://blog.hasmanythrough.com/articles/category/associations](http://blog.hasmanythrough.com/articles/category/associations) , for  
more info.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 6, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/basic-has-many-question/78474/5 "2006-12-06T16:59:52Z")

</div>

If the application requires you to record details that relates the game  
and  
players in the encounter, then you can use has\_many :through. Otherwise  
habtm relationship will suffice.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 7, 2006, 7:50am UTC](https://www.ruby-forum.com/t/basic-has-many-question/78474/6 "2006-12-07T07:50:12Z")

</div>

Actually question leads to more important domain-designing issues:

1. Are players on fixed teams? (this is easier, btw)
2. How do you record wins?

For fixed teams, I would go with:

Class Players  
#this assumes you can be on more than one team  
have\_and\_belong\_to\_many :teams

Class Teams  
have\_and\_belong\_to\_many :players  
have\_and\_belong\_to\_many :matches

Class Matches  
have\_and\_belong\_to\_many :teams

Then the Matches table migration would be:  
t.column :team1\_id, :integer, :null =\> false  
t.column :team2\_id, :integer, :null =\> false  
t.column :winner\_id, :integer, :null =\> false  
t.column :team1\_score, :integer, :null =\> false  
t.column :team2\_score, :integer, :null =\> false  
t.column :played\_at, :datetime, :null =\> false

#plus some foreign key mapping that I can’t be bothered to do

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [December 7, 2006, 7:51am UTC](https://www.ruby-forum.com/t/basic-has-many-question/78474/7 "2006-12-07T07:51:11Z")

</div>

of course those should be ‘has\_and\_belongs\_to\_many’
