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
On 16 Oct 2007, at 15:50, Dave A. 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.
Fred
Frederick C. wrote:
On 16 Oct 2007, at 15:50, Dave A. 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.
Fred
Hi Fred,
Thanks for responding. Do I create that in the model?
On Tue, 2007-10-16 at 16:50 +0200, Dave A. wrote:
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 D.
[email protected]
Trondheim, NO
http://tore.darell.no/
On 16 Oct 2007, at 16:26, Dave A. 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.
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
<% @games.each do |game| %>
-
<%= game.home_team.name %>
<% end %>
Frederick C. wrote:
On 16 Oct 2007, at 16:26, Dave A. 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!
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
Yep, 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. 
On 16 Oct 2007, at 16:38, Dave A. wrote:
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