# Using multiple foreign keys in views

**URL:** <https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400>\
**Category:** Rails\
**Created:** [October 16, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400 "2007-10-16T16:50:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [October 16, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400/1 "2007-10-16T16:50:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [October 16, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400/2 "2007-10-16T16:54:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [October 16, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400/3 "2007-10-16T16:56:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [October 16, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400/4 "2007-10-16T17:02:13Z")

</div>

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.  
removed\_email\_address@domain.invalid  
Trondheim, NO  
[http://tore.darell.no/](http://tore.darell.no/)

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [October 16, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400/5 "2007-10-16T17:33:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [October 16, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400/6 "2007-10-16T17:26:23Z")

</div>

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 %\>

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [October 16, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400/7 "2007-10-16T17:38:02Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [October 16, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400/8 "2007-10-16T18:36:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [October 16, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/using-multiple-foreign-keys-in-views/117400/9 "2007-10-16T17:44:24Z")

</div>

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
