simple active record modeling question

Let's say that I'm trying to model a baseball Game, capturing the
names of the home and away teams within the Game class.

Game has both a (has_a) home_team and an away_team.

one could write the Game class like this:

class Game < ActiveRecord::Base
  has_a home_team_id
  has_a away_team_id
  ...
end

but this necessitates 2 DB tables (home_teams and away_teams) with
duplicated content.

is there a simple railsy way of capturing the fact that Game has both
a home_team and an away_team but use only 1 table (called teams)? I
realize space is cheap these days and i shouldn't fret about
duplication, but perhaps there's a simpler way.

THX!

thebusyant@gmail.com wrote:

Let's say that I'm trying to model a baseball Game, capturing the
names of the home and away teams within the Game class.

Game has both a (has_a) home_team and an away_team.

one could write the Game class like this:

class Game < ActiveRecord::Base
  has_a home_team_id
  has_a away_team_id
  ...
end

but this necessitates 2 DB tables (home_teams and away_teams) with
duplicated content.

is there a simple railsy way of capturing the fact that Game has both
a home_team and an away_team but use only 1 table (called teams)? I
realize space is cheap these days and i shouldn't fret about
duplication, but perhaps there's a simpler way.

THX!

No. you need two foreign keys in games but only one teams model. I
think that something like this is what you need, but this is untested.

class Teams

  has_many :home_games, class => games, :foreign_key =>
home_team_id
  has_many :away_games, class => games, :foreign_key =>
away_team_id

class Games

  belongs_to :team, :foreign_key => home_team_id
  belongs_to :team, :foreign_key => away_team_id

James Byrne wrote:

I said it was untested... snd this probably ahs errors as well. Check
the ActiveRecord::Associations API for details.

Hi --

James Byrne wrote:

I said it was untested... snd this probably ahs errors as well. Check
the ActiveRecord::Associations API for details.

class Teams

  has_many :home_games, :class => games, :foreign_key => :home_team_id

                                 :class_name => "Game"

  has_many :away_games, :class => games, :foreign_key => :away_team_id

class Games

  belongs_to :home_team, :class => :teams, :foreign_key => :home_team_id
  belongs_to :away_team, :class => :teams, :foreign_key => :away_team_id

In Rails 2.0, the foreign key is derived from the association name, so
all you need there is:

   belongs_to :home_team, :class_name => "Team"

and it will figure out that the foreign key is home_team_id (rather
than team_id).

David

thebusyant@gmail.com wrote:

Let's say that I'm trying to model a baseball Game, capturing the
names of the home and away teams within the Game class.

Game has both a (has_a) home_team and an away_team.

one could write the Game class like this:

class Game < ActiveRecord::Base
  has_a home_team_id
  has_a away_team_id
  ...
end

but this necessitates 2 DB tables (home_teams and away_teams) with
duplicated content.

is there a simple railsy way of capturing the fact that Game has both
a home_team and an away_team but use only 1 table (called teams)? I
realize space is cheap these days and i shouldn't fret about
duplication, but perhaps there's a simpler way.

You should fret about mormalizing your data, though. :stuck_out_tongue:

To wit:
Table teams:
id, team

Table games:
id, home_team_id, away_team_id, score_home_team, score_away_team