I have a model, Game, that has_many :teams. There''s a home team and an away team. I created two foreign keys, so each game can have two teams associated with it. Here''s the Team model: class Team < ActiveRecord::Base belongs_to :game, :class_name => ''Game'', :foreign_key => ''hometeam_id'' belongs_to :game, :class_name => ''Game'', :foreign_key => ''visitingteam_id'' end Now, assuming that works correctly, how do I use those associations in my view. Let''s say I want to show the names of the teams in a game. Doing something like this doesn''t work: <%= game.team.name %> because it doesn''t specify which team I''m asking for. How do I differentiate between them when I want to display them in a view? Thanks! Dave -- 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 -~----------~----~----~----~------~----~------~--~---
On 16 Oct 2007, at 15:50, Dave Amos wrote:> > I have a model, Game, that has_many :teams. There''s a home team and an > away team. I created two foreign keys, so each game can have two teams > associated with it. Here''s the Team model: > > class Team < ActiveRecord::Base > belongs_to :game, > :class_name => ''Game'', :foreign_key => ''hometeam_id'' > belongs_to :game, > :class_name => ''Game'', :foreign_key => ''visitingteam_id'' > end > > Now, assuming that works correctly, how do I use those associations in > my view. Let''s say I want to show the names of the teams in a game. > Doing something like this doesn''t work: <%= game.team.name %> > because it > doesn''t specify which team I''m asking for. How do I differentiate > between them when I want to display them in a view? >You need to give your associations different names, eg :home_team, :visiting_team. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 16 Oct 2007, at 15:50, Dave Amos wrote: > >> end >> >> Now, assuming that works correctly, how do I use those associations in >> my view. Let''s say I want to show the names of the teams in a game. >> Doing something like this doesn''t work: <%= game.team.name %> >> because it >> doesn''t specify which team I''m asking for. How do I differentiate >> between them when I want to display them in a view? >> > > You need to give your associations different names, > eg :home_team, :visiting_team. > > FredHi Fred, Thanks for responding. Do I create that in the model? -- 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 -~----------~----~----~----~------~----~------~--~---
On Tue, 2007-10-16 at 16:50 +0200, Dave Amos wrote:> I have a model, Game, that has_many :teams. There''s a home team and an > away team. I created two foreign keys, so each game can have two teams > associated with it. Here''s the Team model: > > class Team < ActiveRecord::Base > belongs_to :game, > :class_name => ''Game'', :foreign_key => ''hometeam_id'' > belongs_to :game, > :class_name => ''Game'', :foreign_key => ''visitingteam_id'' > end > > Now, assuming that works correctly, how do I use those associations in > my view. Let''s say I want to show the names of the teams in a game. > Doing something like this doesn''t work: <%= game.team.name %> because it > doesn''t specify which team I''m asking for. How do I differentiate > between them when I want to display them in a view?Use different names for the associations like :home_game and :away_game -- Tore Darell toredarell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Trondheim, NO http://tore.darell.no/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Does this look right? It doesn''t seem to work. class Team < ActiveRecord::Base belongs_to :home_team, :class_name => ''Game'', :foreign_key => ''hometeam_id'' belongs_to :away_team, :class_name => ''Game'', :foreign_key => ''visitingteam_id'' end ---- class Game < ActiveRecord::Base has_many :home_teams, :class_name => ''Team'', :foreign_key => ''hometeam_id'' has_many :away_teams, :class_name => ''Team'', :foreign_key => ''visitingteam_id'' end ---- Then, in localhost/games/index.rhtml <ul> <% @games.each do |game| %> <li> <%= game.home_team.name %> </li> <% end %> </ul> -- 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 -~----------~----~----~----~------~----~------~--~---
On 16 Oct 2007, at 16:26, Dave Amos wrote:> > Does this look right? It doesn''t seem to work. >What do you mean by doesn''t work? It seems counter intuitive that a game would have many home/away teams, I would expect game belongs_to :away_team and game belongs to home_team, and then team has_many away_games, team has_many home_games but if this is the way your data really is then I can''t argue with that Fred.> class Team < ActiveRecord::Base > belongs_to :home_team, > :class_name => ''Game'', :foreign_key => ''hometeam_id'' > belongs_to :away_team, > :class_name => ''Game'', :foreign_key => ''visitingteam_id'' > end > > ---- > > class Game < ActiveRecord::Base > has_many :home_teams, > :class_name => ''Team'', :foreign_key => ''hometeam_id'' > has_many :away_teams, > :class_name => ''Team'', :foreign_key => ''visitingteam_id'' > end > > ---- > > Then, in localhost/games/index.rhtml > > <ul> > <% @games.each do |game| %> > <li> > <%= game.home_team.name %> > </li> > <% end %> > </ul> > -- > 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 16 Oct 2007, at 16:26, Dave Amos wrote: > >> >> Does this look right? It doesn''t seem to work. >> > What do you mean by doesn''t work? > It seems counter intuitive that a game would have many home/away > teams, I would expect > game belongs_to :away_team and game belongs to home_team, and then > team has_many away_games, team has_many home_games > but if this is the way your data really is then I can''t argue with that > > Fred.I see what you''re saying. I guess to me the games are more important than the teams, so I may have structured it backwards. :) I''ll mess around with it and see what happens. Thanks! -- 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 -~----------~----~----~----~------~----~------~--~---
On 16 Oct 2007, at 16:38, Dave Amos wrote:> > Frederick Cheung wrote: >> On 16 Oct 2007, at 16:26, Dave Amos wrote: >> >>> >>> Does this look right? It doesn''t seem to work. >>> >> What do you mean by doesn''t work? >> It seems counter intuitive that a game would have many home/away >> teams, I would expect >> game belongs_to :away_team and game belongs to home_team, and then >> team has_many away_games, team has_many home_games >> but if this is the way your data really is then I can''t argue with >> that >> >> Fred. > > I see what you''re saying. I guess to me the games are more important > than the teams, so I may have structured it backwards. :) I''ll mess > around with it and see what happens. Thanks!It''s just a question of what the relationships are. In most sports I know of, a game will involve a home team and a away team, and over the course of a season a team will play many away games and many team games. from that your game table pretty much has to have a home team id and an away team id, which leads to class Game < AR:Base belongs_to :away_team, ... belongs_to :home_team, ... end class Team < AR:Base has_many :home_games, ... has_many :away_games, ... end Fred> -- > 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 -~----------~----~----~----~------~----~------~--~---
> class Game < AR:Base > belongs_to :away_team, ... > belongs_to :home_team, ... > end > > class Team < AR:Base > has_many :home_games, ... > has_many :away_games, ... > end > > FredYep, you''re right. I fixed it and it works like a charm. Thanks so much for humoring me while I got it straight in my head. :) -- 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 -~----------~----~----~----~------~----~------~--~---