Help needed setting up a simple app

A team has many players, and players belong to only one team. So in your Team class, add this line:

has_many :players

and in your Player class add this line:

belongs_to :team

And in your players table, have an int column named team_id

That's it. Rails will handle the relationship. You'll now magically have an array in Team called players. To assign a player to a team, just write something like this:

team = Team.find_by_name("Gorillas") #for example = team

Now you've got a different relationship, because before players could have only one team, but they can have many games and also belong to many games.

So in Game you add

has_and_belongs_to_many :players

and in Player you add

has_and_belongs_to_many :games

For this type of relationship neither your games table nor your players table gets a foreign key. (In the Teams-Players question the players table had a team_id column). So you have to create a third table to allow rails to manage the relationship. The name of the table should be the two other tables in alphabetical order, separated by an underscore. So in this case, that table is named games_players, and contains a game_id column and a player_id column.

Now a player magically has an array called games, and a game has an array called players. To add a player to a game, write it like this: some_game.players << this_player. Or you can do it the other way around. It doesn't matter, because what will happen is that rails will add to that join table a row containing both IDs.

But this is just scratching the surface. There is much more you can do with a HABTM relationship. Googling has_and_belongs_to_many will get you a whole bunch of good articles.

Also, if you still have teams that part doesn't change. A player can both belongs_to :team and has_and_belongs_to_many :games.

Where is that array? Trying to access that in a controller/view throws an error saying the array is a nil object...

It shouldn't. Can you post the error?

is using has_many: players, :through => :scores better than has_and_belongs_to_many?

Only if scores have players, which seems odd to me.

Parked at Loopia

How are you setting @players?

Somewhere you ought to have something like @players = my_game.players. Or, if you've already got an @game, just put @game.players in there.

Surely a good way to link players to games would be to create scores that map to a player and a game... means lots of games with however many players you want, each with 1 score per game. how should i be doing it? --

I would think the score would be part of the definition of a game. Just for an example, assuming a game had a score, and I wanted to find a player's average score, I could write

total = 0{ |game| total += game.score } avg_score = total/

(you might check for a size of zero before dividing, of course)

Oh I see. I was thinking of a game with one score, but you're talking about each player having his own score within a game (or event).

One approach would be to expand the join table to be model as well, which includes those stats.

If that model were called, say, called IndividualResult, then Game

has_many : individual_results has_many :players, :through => :individual_results

and the other way around for Player.

IndividualResult's table would look like this:

game_id player_id score fleeb_attempts successful_fleebs etc.

And of course IndividualResult would

belongs_to :game belongs_to :player