views:

26

answers:

1

I have three models in a Rails application: Game represents an instance of a game being played. Player represents an instance of a participant in a game. User represents a registered person who can participate in games.

Each Game can have many Players, and each User can have many Players (a single person can participate in multiple games at once); but each Player is in precisely one Game, and represents precisely one User. Hence, my relationships are as follows at present.

class Game
  has_many :players
end

class User
  has_many :players
end

class Player
  belongs_to :game
  belongs_to :user
end

... where naturally the players table has game_id and user_id columns, but games and users have no foreign keys.

I would also like to represent the fact that each Game has many Users playing in it; and each User has many Games in which they are playing. How do I do this? Is it enough to add

class Game
  has_many :users, :through => :players
end

class User
  has_many :games, :through => :players
end
+1  A: 

What you propose should be enough; Now, each game should have a users[] array, and each user has a games[] array.

http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

:has_many :through is basically an extension of the typical many-to-many model where you have an intermediate table with ids for each one of the related entities. However, in Rails, the :has_and_belongs_to_many relationship doesn't allow for the intermediate table to be an entity in itself, in that it has no meaning other than serving as glue between the two related models.

:has_many :through allows a real entity or model in the application (in your case, players) to act as "glue" between two others (games, users) but also allowing you to manipulate "players" as in this case it does contain important information.

Hope this helps.

Roadmaster
Can't believe I've not come across the Guides before! I've basically be working from "agile web dev", which mentions the article->section->paragraph case, but doesn't really cover the "HABTM with a real middle" case.
Chris