# Need help for Best Approach to Model

**URL:** <https://www.ruby-forum.com/t/need-help-for-best-approach-to-model/154208>\
**Category:** Rails\
**Created:** [November 26, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/need-help-for-best-approach-to-model/154208 "2008-11-26T12:56:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![srj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@srj](https://www.ruby-forum.com/u/srj)\
**Post date:** [November 26, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/need-help-for-best-approach-to-model/154208/1 "2008-11-26T12:56:39Z")

</div>

Hello -

I am new to Rails (and Ruby), and would appreciate some guidance on  
the best way to model this situation:

I am creating a game website, that will require users to register on  
the site before they can play the game. I may have multiple instances  
of the game running at any given time. A registered user must also  
register for the game(s) they want to play. So far, I have the  
following models & relationships:

Game  
has\_many :players (:users???)  
has\_one :prize

User  
belongs\_to :game ???

Prize  
belongs\_to :game

When a User registers for a Game, I want to create a record that will  
store progress information for that User, for that Game (a User may be  
registered for multiple games). The key to this record may be game\_id  
& user\_id. Or, the key may be an auto-generated integer, but if so,  
then I think I need to hold onto game\_id & user\_id as attributes.

I am trying to figure out the best “rails way” to model this using  
Rails 2.1.

Do I need a Player model, or will that be maintained via the  
relationships?

Player  
belongs\_to :game ???  
has\_one :user ???

Thanks for any help that can be offered.

---

<div class="post-metadata">

**Author:** ![srj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@srj](https://www.ruby-forum.com/u/srj)\
**Post date:** [November 26, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/need-help-for-best-approach-to-model/154208/2 "2008-11-26T13:46:03Z")

</div>

You will most likely need an intermediate table like UserGames  
(or PlayerGames if you prefer)

Game:  
has\_many :user\_games  
has\_many :users, :through =\> :user\_games

User:  
has\_many :user\_games  
has\_many :games, :through =\> :user\_games

this requires the table UserGames with the user\_id and game\_id fields

the second association definition will allow you to get all  
users in a game (or all games of a user) like:

@user = User.find(:first,…)  
@user.games
