# Has\_and\_belongs\_to\_many question

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-question/105523>\
**Category:** Rails\
**Created:** [May 30, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/105523 "2007-05-30T18:43:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![uli\_kunkel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/f17d59/32.png) [@uli\_kunkel](https://www.ruby-forum.com/u/uli_kunkel)\
**Post date:** [May 30, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/105523/1 "2007-05-30T18:43:29Z")

</div>

Hi,

I have a has\_and\_belongs\_to\_many relationship between games and users in  
my project. Now i want to save the rank a user had each game he played.  
It seems obvious to add a field in the table linking the two holding the  
rank. Only im having problems making this happen. Creating a model of  
this table makes ruby think the corresponding table is game\_users, and  
ruby also needs an id field in this table.  
Probably some really easy settings to fix this, but i cant find it  
anywhere so any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![uli\_kunkel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/f17d59/32.png) [@uli\_kunkel](https://www.ruby-forum.com/u/uli_kunkel)\
**Post date:** [May 30, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/105523/2 "2007-05-30T19:45:44Z")

</div>

Uli,

If you want to have some properties in your association, you may create  
a model for that.  
Remove your games\_users table (I guess from your Game or User migration  
file), and create a model.  
Add game\_id, user\_id and rank in your migration file.

Then add belongs\_to :user and belogns\_to :game in this model. Last, add  
has\_many in User and Game model.

If you want to access users from Game and games from User, just add an  
has\_many :through (check the doc).

Regards,  
Jean-Etienne

> **[Domain2.hu](https://domain2.hu/www.woa.hu)**
>
> www.woa.hu megvásárolható domain a kiemelt domain nevek piacterén.

---

<div class="post-metadata">

**Author:** ![uli\_kunkel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/f17d59/32.png) [@uli\_kunkel](https://www.ruby-forum.com/u/uli_kunkel)\
**Post date:** [May 31, 2007, 11:09am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-question/105523/3 "2007-05-31T11:09:06Z")

</div>

Jean is right. If you want to include information with your  
associations, you can’t use HABTM anymore. You have to make your HABTM  
table into a normal table and associate the other tables with each other  
“:through” that table instead.
