# Has and belongs to many

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many/134097>\
**Category:** Rails\
**Created:** [April 7, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many/134097 "2008-04-07T21:35:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many/134097/1 "2008-04-07T21:35:59Z")

</div>

Hello, I’ve searched but I can’t find a good tutorial to has and  
belongs to many.

What should both of my models, Games and Players, look like if a game  
has many players and a player has many games.

Also when I create I want to be able to associate the player with the  
game. Before I was doing  
@game.save  
for player in @home.players  
player.game\_id = @game  
player.increment! :games  
end

What should I do now to make the association? Also how do I find all  
players by game\_id?

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many/134097/2 "2008-04-07T21:41:19Z")

</div>

Doing player.game\_ids = @game gives  
undefined method `reject’ for 1:Fixnum

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many/134097/3 "2008-04-07T21:44:50Z")

</div>

Also why is increment! not working.  
By the way @home = Team.find\_by\_user\_id(current\_user.id)  
and team has many players.

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many/134097/4 "2008-04-07T21:49:30Z")

</div>

So if I have a Game a Player and a Team I would want to do  
Game has many teams (actually only 2)  
Team has many games  
all through a Player???

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many/134097/5 "2008-04-07T21:43:39Z")

</div>

> Hello, I’ve searched but I can’t find a good tutorial to has and  
> belongs to many.
> 
> What should both of my models, Games and Players, look like if a game  
> has many players and a player has many games.

[http://mboffin.com/stuff/ruby-on-rails-data-relationships.png](http://mboffin.com/stuff/ruby-on-rails-data-relationships.png)

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many/134097/6 "2008-04-07T22:42:00Z")

</div>

On Apr 7, 2:48 pm, edberner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > What should both of my models, Games and Players, look like if a game  
> > > > has many players and a player has many games.

The key is, a game is a connection between two teams. So a Games  
table would consist of three columns: id, home\_team\_id, away\_team\_id:

class Game  
belongs\_to :home\_team, :class\_name =\> “Team”  
belongs\_to :away\_team, :class\_name =\> “Team”  
end

class Player  
belongs\_to :team  
end

class Team  
has\_many :games  
has\_many :players  
end

For example:

cubs = Team.find(1)  
sox = Team.find(2)

g = Game.create(:home\_team =\> cubs, :away\_team =\> sox)

g.home\_team.players # shows players on the Cubs

p = cubs.players.first  
p.team.games # shows all the games this player is in

Of course, that last bit assumes the player plays in every game the  
team plays in. If that’s not the case for you, you would need to add  
another table to tracks ‘attendance’ of the player for each game.

So I’ve just shown the simple case here, in the hopes it gives you a  
start in the right direction or is at least food for thought.

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software
