Working through my first RoR project, I''m trying to puzzle my way through table associations. It seems that associations can be done multiple ways and I''m looking for what is considered the most "correct." I could probably brute-force my way through this, but I doubt I''d end up with the most elegant solution. I have three tables: Players: stores information about players players.id players.name Games: stores information about games games.id games.name Games_Players: links players with games player_id game_id A player will only appear once in Games_Players, but a game will appear multiple times. I want to be able to retrieve: If a player has picked a game What game a player has picked How many players a game has What players have picked a game Below are the associations I''ve put in so far. What would you do? What''s the simplest way to associate these tables? Games has_and_belongs_to_many :players Games_Players belongs_to: game belongs_to: player Thanks, Ian
On Wed, 30 Mar 2005 11:12:20 -0600, Ian Whitney <iwhitney-lJS9ZXtlhXvYtjvyW6yDsg@public.gmane.org> wrote:> Working through my first RoR project, I''m trying to puzzle my way > through table associations. It seems that associations can be done > multiple ways and I''m looking for what is considered the most > "correct." I could probably brute-force my way through this, but I > doubt I''d end up with the most elegant solution. > > I have three tables: > > Players: stores information about players > players.id > players.name > > Games: stores information about games > games.id > games.name > > Games_Players: links players with games > player_id > game_id > > A player will only appear once in Games_Players, but a game will appear > multiple times. > > I want to be able to retrieve: > > If a player has picked a game > What game a player has picked > How many players a game has > What players have picked a game > > Below are the associations I''ve put in so far. What would you do? > What''s the simplest way to associate these tables? > > Games > has_and_belongs_to_many :players > > Games_Players > belongs_to: game > belongs_to: player > > Thanks, > > Ian >Hi Ian If I were you, I''d ditch the Games_Players table for this situation. Really, you have: class Player < ActiveRecord::Base belongs_to :game end class Game < ActiveRecord::Base has_many :players end Your player table would include a game_id column. That''s all you''d need. Dave -- Dave Goodlad dgoodlad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org or dave-eHfbeeWWzZOw5LPnMra/2Q@public.gmane.org http://david.goodlad.ca/
>> Working through my first RoR project, I''m trying to puzzle my way >> through table associations. It seems that associations can be done >> multiple ways and I''m looking for what is considered the most >> "correct." I could probably brute-force my way through this, but I >> doubt I''d end up with the most elegant solution. >> >> etc. etc. > > Hi Ian > > If I were you, I''d ditch the Games_Players table for this situation. > Really, you have: > > class Player < ActiveRecord::Base > belongs_to :game > end > class Game < ActiveRecord::Base > has_many :players > end > > Your player table would include a game_id column. That''s all you''d > need. > > Dave > > -- > Dave Goodlad > dgoodlad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org or dave-eHfbeeWWzZOw5LPnMra/2Q@public.gmane.org > http://david.goodlad.ca/This is true, but I''m also working with the expectation that eventually a player will be able to have multiple games. By using the linking table now, I''m hoping to avoid a headache later.
On Wed, 30 Mar 2005 11:31:04 -0600, Ian Whitney <iwhitney-lJS9ZXtlhXvYtjvyW6yDsg@public.gmane.org> wrote:> >> Working through my first RoR project, I''m trying to puzzle my way > >> through table associations. It seems that associations can be done > >> multiple ways and I''m looking for what is considered the most > >> "correct." I could probably brute-force my way through this, but I > >> doubt I''d end up with the most elegant solution. > >> > >> etc. etc. > > > > Hi Ian > > > > If I were you, I''d ditch the Games_Players table for this situation. > > Really, you have: > > > > class Player < ActiveRecord::Base > > belongs_to :game > > end > > class Game < ActiveRecord::Base > > has_many :players > > end > > > > Your player table would include a game_id column. That''s all you''d > > need. > > > > Dave > > > > -- > > Dave Goodlad > > dgoodlad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org or dave-eHfbeeWWzZOw5LPnMra/2Q@public.gmane.org > > http://david.goodlad.ca/ > > This is true, but I''m also working with the expectation that eventually > a player will be able to have multiple games. By using the linking > table now, I''m hoping to avoid a headache later. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Ok, in that case, I''d drop the actual model object ''Games_Players'' - it''s not necessary. The fact that the table exists to link games and players tables is sufficient.> I want to be able to retrieve: > If a player has picked a gameplayer.games.empty? should do it> What game a player has pickedplayer.games will return the array of games that a player is associated with> How many players a game hasgame.players.length> What players have picked a gamegame.players HTH! Dave -- Dave Goodlad dgoodlad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org or dave-eHfbeeWWzZOw5LPnMra/2Q@public.gmane.org http://david.goodlad.ca/
On Wed, 30 Mar 2005 11:12:20 -0600, Ian Whitney <iwhitney-lJS9ZXtlhXvYtjvyW6yDsg@public.gmane.org> wrote:> Working through my first RoR project, I''m trying to puzzle my way > through table associations. It seems that associations can be done > multiple ways and I''m looking for what is considered the most > "correct." I could probably brute-force my way through this, but I > doubt I''d end up with the most elegant solution. > > I have three tables: > > Players: stores information about players > players.id > players.name > > Games: stores information about games > games.id > games.name > > Games_Players: links players with games > player_id > game_id > > A player will only appear once in Games_Players, but a game will appear > multiple times. > > I want to be able to retrieve: > > If a player has picked a game > What game a player has picked > How many players a game has > What players have picked a game > > Below are the associations I''ve put in so far. What would you do? > What''s the simplest way to associate these tables? > > Games > has_and_belongs_to_many :players > > Games_Players > belongs_to: game > belongs_to: player > > Thanks,There is no model for Games_Players. You should just need: class Game < ActiveRecord::Base has_and_belongs_to_many :players end class Player < ActiveRecord::Base has_and_belongs_to_many :games end Excuse me if the syntax is slightly incorrect, I''m typing this from memory :) -- rick http://techno-weenie.net
> There is no model for Games_Players. You should just need: > > class Game < ActiveRecord::Base > has_and_belongs_to_many :players > end > > class Player < ActiveRecord::Base > has_and_belongs_to_many :games > endYou do still want your games_players table around, though. It''s merely not its own model.