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!

[email protected] 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 B. 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
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

Hi –

On Fri, 11 Apr 2008, James B. wrote:

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


Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.rubypal.com for details and updates!

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[email protected] 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

########
home_team_id = teams.id (foreign key constraint)
away_team = teams.id (foreign key constraint)


Phillip G.
Twitter: twitter.com/cynicalryan

~ I don’t need to compromise my principles, because they don’t have
the
slightest bearing on what happens to me anyway. – Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkf/u8oACgkQbtAgaoJTgL9rJQCffU1I1f1CaEMiQ9mlQYIs8gPo
6zEAoKHOh7n+YJBdBUTaaMYJ7FfoMOWW
=YoWf
-----END PGP SIGNATURE-----