Forum: Ruby on Rails Need help for Best Approach to Model

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
srj (Guest)
on 2008-11-26 13:56
(Received via mailing list)
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:

  has_many :players (:users???)
  has_one :prize

  belongs_to :game ???

  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

  belongs_to :game ?????
  has_one :user ?????

Thanks for any help that can be offered.
Thorsten M. (Guest)
on 2008-11-26 14:46
(Received via mailing list)
You will most likely need an intermediate table like UserGames
(or PlayerGames if you prefer)

has_many :user_games
has_many :users, :through => :user_games

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,...)
This topic is locked and can not be replied to.