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.
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
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.