# \[newbie\] double relationships in database

**URL:** <https://www.ruby-forum.com/t/newbie-double-relationships-in-database/172889>\
**Category:** Rails\
**Created:** [July 29, 2009, 1:43pm UTC](https://www.ruby-forum.com/t/newbie-double-relationships-in-database/172889 "2009-07-29T13:43:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jask](https://www.ruby-forum.com/u/jask)\
**Post date:** [July 29, 2009, 1:43pm UTC](https://www.ruby-forum.com/t/newbie-double-relationships-in-database/172889/1 "2009-07-29T13:43:43Z")

</div>

I have the following tables  
teams  
id :string  
name :string

and

matches  
id:  
home\_team :team\_id  
visitor\_team :team\_id

how can I reflect that kind of relationship in a RoR model?  
thanks and keep up the good work.

---

<div class="post-metadata">

**Author:** ![jask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jask](https://www.ruby-forum.com/u/jask)\
**Post date:** [July 29, 2009, 2:11pm UTC](https://www.ruby-forum.com/t/newbie-double-relationships-in-database/172889/2 "2009-07-29T14:11:49Z")

</div>

Hi! Try this:

class Team \< AR::B  
has\_many :home\_matches, :class\_name =\> ‘Match’, :foreign\_key  
=\> :home\_team  
has\_many :visit\_matches, :class\_name =\> ‘Match’, :foreign\_key  
=\> :visit\_team  
end

class Match \< AR::B  
belongs\_to :home\_team, :class\_name =\> ‘Team’  
belongs\_to :visit\_team, :class\_name =\> ‘Team’  
end

---

<div class="post-metadata">

**Author:** ![jask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jask](https://www.ruby-forum.com/u/jask)\
**Post date:** [July 29, 2009, 1:58pm UTC](https://www.ruby-forum.com/t/newbie-double-relationships-in-database/172889/3 "2009-07-29T13:58:49Z")

</div>

jask wrote:

> I have the following tables  
> teams  
> id :string  
> name :string
> 
> and
> 
> matches  
> id:  
> home\_team :team\_id  
> visitor\_team :team\_id
> 
> how can I reflect that kind of relationship in a RoR model?  
> thanks and keep up the good work.

teams  
id :integer  
name :string

matches  
id :integer  
team\_id :integer  
visitor\_team :integer  
date\_scheduled :datetime

class Team \< ActiveRecord::Base  
has\_many :matches  
has\_many :visitor\_teams, :through =\> :matches  
end

class Match \< ActiveRecord::Base  
belongs\_to :team  
belongs\_to :visitor\_team, :class\_name =\> “Team”  
end

The home team goes in your team\_id column which is associated with  
team.id. You create a self-referential association to the visitor\_team.

Then you call them using:

@matches = Match.find(:all, :joins =\> [:team, :visitor\_team], :order =\>  
:date\_scheduled)

Retrieve the data:

@matches.each do |match|  
match.team.name  
match.visitor\_team.name  
match.date\_scheduled  
end

You really should learn about associations before you go through all  
this trouble. You can check out [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/) .

It just so happens that I run a football site so I do pretty much  
exactly what you are after. If you have some questions, I’ll be happy  
to help.

---

<div class="post-metadata">

**Author:** ![jask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jask](https://www.ruby-forum.com/u/jask)\
**Post date:** [July 29, 2009, 2:11pm UTC](https://www.ruby-forum.com/t/newbie-double-relationships-in-database/172889/4 "2009-07-29T14:11:49Z")

</div>

On Jul 29, 2:05 am, jask [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> visitor\_team :team\_id
> 
> how can I reflect that kind of relationship in a RoR model?  
> thanks and keep up the good work.

See

> **[Creating multiple associations with the same table - Space Vatican](https://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table/)**
>
> Rails makes setting up your associations dead easy and very concise. There’s a whole bunch of options that can be set however as long as you …

Fred

---

<div class="post-metadata">

**Author:** ![jask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jask](https://www.ruby-forum.com/u/jask)\
**Post date:** [July 30, 2009, 6:42am UTC](https://www.ruby-forum.com/t/newbie-double-relationships-in-database/172889/5 "2009-07-30T06:42:22Z")

</div>

Thanks man, maybe I’ll take your word for help?  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![jask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jask](https://www.ruby-forum.com/u/jask)\
**Post date:** [July 30, 2009, 6:41am UTC](https://www.ruby-forum.com/t/newbie-double-relationships-in-database/172889/6 "2009-07-30T06:41:47Z")

</div>

Thanks to everyone, your information has been most usefull
