# Include with two references of one model of the same table

**URL:** <https://www.ruby-forum.com/t/include-with-two-references-of-one-model-of-the-same-table/51978>\
**Category:** Rails\
**Created:** [January 9, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/include-with-two-references-of-one-model-of-the-same-table/51978 "2006-01-09T22:18:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [January 9, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/include-with-two-references-of-one-model-of-the-same-table/51978/1 "2006-01-09T22:18:44Z")

</div>

Hey guys,

```
I just came across this oddity, not sure what to make of it yet, but

```

I think it might be incorrect behavior. When doing a @inst.find(:all,  
:include =\> [“hometeam”, “awayteam”] …) hometeam and awayteam are two  
references from a belongs\_to that is of the same model and of the same  
table. I will get a pgerror stating that “matches”, which is the table  
that these fk’s loop back to is being ambiguously referenced. The SQL  
spit out showed only one select on “matches” where I think should be two  
selects on two aliases of “matches”.

Heres some code below to better understand what I’m talking about.

class Match \< ActiveRecord::Base  
belongs\_to :hometeam, :class\_name =\> “TeamsSeason”, :foreign\_key =\>  
“hometeam\_id”  
belongs\_to :awayteam, :class\_name =\> “TeamsSeason”, :foreign\_key =\>  
“awayteam\_id”  
end

class TeamsSeason \< ActiveRecord::Base  
has\_many :hometeam, :class\_name =\> “Match”, :foreign\_key =\>  
“hometeam\_id”, :dependent =\> :destroy  
has\_many :awayteam, :class\_name =\> “Match”, :foreign\_key =\>  
“awayteam\_id”, :dependent =\> :destroy

def self.find\_all\_played id  
find(:all, :include =\> [“hometeam”, “awayteam”])  
end  
end

Heres a sample of the error…

|PGError: ERROR: table name “matches” specified more than once  
: SELECT matches.“awayteam\_goals” AS t2\_r1, matches.“season\_id” AS  
t1\_r7, teams\_seasons.“division\_id” AS t0\_r6, matches.“hometeam\_goals” AS  
t2\_r2, matches.“league\_id” AS t1\_r8, matches.“awayteam\_id” AS t2\_r3,  
matches.“venue\_id” AS t1\_r9, matches.“hometeam\_id” AS t2\_r4,  
matches.“matchtime\_start” AS t2\_r5, matches.“id” AS t1\_r0,  
matches.“matchtime\_stop” AS t2\_r6, matches.“awayteam\_goals” AS t1\_r1,  
teams\_seasons.“id” AS t0\_r0, matches.“season\_id” AS t2\_r7,  
matches.“hometeam\_goals” AS t1\_r2, teams\_seasons.“team\_id” AS t0\_r1,  
matches.“league\_id” AS t2\_r8, matches.“awayteam\_id” AS t1\_r3,  
teams\_seasons.“season\_id” AS t0\_r2, matches.“venue\_id” AS t2\_r9,  
matches.“division\_id” AS t1\_r10, matches.“hometeam\_id” AS t1\_r4,  
teams\_seasons.“coach\_id” AS t0\_r3, matches.“division\_id” AS t2\_r10,  
matches.“id” AS t2\_r0, matches.“played” AS t1\_r11,  
matches.“matchtime\_start” AS t1\_r5, teams\_seasons.“manager\_id” AS t0\_r4,  
matches.“played” AS t2\_r11, matches.“matchtime\_stop” AS t1\_r6,  
teams\_seasons.“league\_id” AS t0\_r5 FROM teams\_seasons LEFT OUTER JOIN  
matches ON matches.hometeam\_id = teams\_seasons.id LEFT OUTER JOIN  
matches ON matches.awayteam\_id = teams\_seasons.id WHERE  
(teams\_seasons.division\_id = 1) |

I’d like to know if this is an incorrect implementation detail or just a  
_don’t\_do\_that_ problem =)

## –

Adam B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Integrum Technologies, LLC  
Phone: +1 602 792 1270  
Mobile: +1 480 580 7091

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [January 28, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/include-with-two-references-of-one-model-of-the-same-table/51978/2 "2006-01-28T23:36:57Z")

</div>

I am having the same probem…

My code looks like this

class Flight \< ActiveRecord::Base  
belongs\_to :airport\_of\_orgin,  
:class\_name =\> “Airport”, :foreign\_key =\> “orignal\_airport\_id”  
belongs\_to :airport\_of\_destine,  
:class\_name =\> “Airport”, :foreign\_key =\> “destination\_airport\_id”  
end

Is there any method to solve is problem?

regards,  
Leon.

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [January 29, 2006, 12:16am UTC](https://www.ruby-forum.com/t/include-with-two-references-of-one-model-of-the-same-table/51978/3 "2006-01-29T00:16:12Z")

</div>

Chad F. on irc suggest to use :joins to solve the problemm…

using joins…

look out for his book [http://www.pragmaticprogrammer.com/titles/fr\_rr/](http://www.pragmaticprogrammer.com/titles/fr_rr/)

regards,  
Leon…

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [January 29, 2006, 2:13am UTC](https://www.ruby-forum.com/t/include-with-two-references-of-one-model-of-the-same-table/51978/4 "2006-01-29T02:13:37Z")

</div>

Yes, doing the joins manually is the best solution right now until AR  
figures out how to detect identical tables and alias them automatically.

Bob S.

[http://www.railtie.net/](http://www.railtie.net/)

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Leon L.  
Sent: Saturday, January 28, 2006 3:15 PM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Rails] Include with two references of one model of the  
same  
table

Chad F. on irc suggest to use :joins to solve the problemm…

using joins…

look out for his book [http://www.pragmaticprogrammer.com/titles/fr\_rr/](http://www.pragmaticprogrammer.com/titles/fr_rr/)

regards,  
Leon…

On 1/28/06, Leon L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

I am having the same probem…

My code looks like this

class Flight \< ActiveRecord::Base  
belongs\_to :airport\_of\_orgin,  
:class\_name =\> “Airport”, :foreign\_key =\> “orignal\_airport\_id”  
belongs\_to :airport\_of\_destine,  
:class\_name =\> “Airport”, :foreign\_key =\> “destination\_airport\_id”  
end

Is there any method to solve is problem?

regards,  
Leon.

On 1/9/06, Adam B. \< removed\_email\_address@domain.invalid  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \> wrote:

Hey guys,

```
I just came across this oddity, not sure what to make of it yet, but

```

I think it might be incorrect behavior. When doing a @inst.find(:all,  
:include =\> [“hometeam”, “awayteam”] …) hometeam and awayteam are two  
references from a belongs\_to that is of the same model and of the same  
table. I will get a pgerror stating that “matches”, which is the table  
that these fk’s loop back to is being ambiguously referenced. The SQL  
spit out showed only one select on “matches” where I think should be two  
selects on two aliases of “matches”.

Heres some code below to better understand what I’m talking about.

class Match \< ActiveRecord::Base  
belongs\_to :hometeam, :class\_name =\> “TeamsSeason”, :foreign\_key =\>  
“hometeam\_id”  
belongs\_to :awayteam, :class\_name =\> “TeamsSeason”, :foreign\_key =\>  
“awayteam\_id”  
end

class TeamsSeason \< ActiveRecord::Base  
has\_many :hometeam, :class\_name =\> “Match”, :foreign\_key =\>  
“hometeam\_id”, :dependent =\> :destroy  
has\_many :awayteam, :class\_name =\> “Match”, :foreign\_key =\>  
“awayteam\_id”, :dependent =\> :destroy

def self.find\_all\_played id  
find(:all, :include =\> [“hometeam”, “awayteam”])  
end  
end

Heres a sample of the error…

|PGError: ERROR: table name “matches” specified more than once  
: SELECT matches.“awayteam\_goals” AS t2\_r1, matches.“season\_id” AS  
t1\_r7,  
teams\_seasons.“division\_id” AS t0\_r6, matches.“hometeam\_goals” AS t2\_r2,  
matches.“league\_id” AS t1\_r8, matches.“awayteam\_id” AS t2\_r3,  
matches.“venue\_id” AS t1\_r9, matches.“hometeam\_id” AS t2\_r4,  
matches.“matchtime\_start” AS t2\_r5, matches.“id” AS t1\_r0,  
matches.“matchtime\_stop” AS t2\_r6, matches.“awayteam\_goals” AS t1\_r1,  
teams\_seasons.“id” AS t0\_r0, matches.“season\_id” AS t2\_r7,  
matches.“hometeam\_goals” AS t1\_r2, teams\_seasons.“team\_id” AS t0\_r1,  
matches.“league\_id” AS t2\_r8, matches.“awayteam\_id” AS t1\_r3,  
teams\_seasons.“season\_id” AS t0\_r2, matches.“venue\_id” AS t2\_r9,  
matches.“division\_id” AS t1\_r10, matches.“hometeam\_id” AS t1\_r4,  
teams\_seasons.“coach\_id” AS t0\_r3, matches.“division\_id” AS t2\_r10,  
matches.“id” AS t2\_r0, matches.“played” AS t1\_r11,  
matches.“matchtime\_start”  
AS t1\_r5, teams\_seasons.“manager\_id” AS t0\_r4, matches.“played” AS  
t2\_r11,  
matches.“matchtime\_stop” AS t1\_r6, teams\_seasons.“league\_id” AS t0\_r5  
FROM  
teams\_seasons LEFT OUTER JOIN matches ON matches.hometeam\_id =  
teams\_seasons.id LEFT OUTER JOIN matches ON matches.awayteam\_id =  
teams\_seasons.id WHERE (teams\_seasons.division\_id = 1) |

I’d like to know if this is an incorrect implementation detail or just a  
_don’t\_do\_that_ problem =)

## –

Adam B. \<removed\_email\_address@domain.invalid \>  
Integrum Technologies, LLC  
Phone: +1 602 792 1270  
Mobile: +1 480 580 7091

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
First they laugh at you, then they ignore you, then they fight you.  
Then you win.  
– Mahatma Karamchand Gandhi
