thebusyant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Apr-11 19:03 UTC
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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
thebusyant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 > 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi -- On Fri, 11 Apr 2008, James Byrne wrote:> > 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_idIn 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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 thebusyant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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. :P 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 Gawlowski 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----- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---