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.
on 2008-11-26 13:56
on 2008-11-26 14:46
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