# Is it possible for table intersection reference same table

**URL:** <https://www.ruby-forum.com/t/is-it-possible-for-table-intersection-reference-same-table/144668>\
**Category:** Rails\
**Created:** [August 1, 2008, 1:42am UTC](https://www.ruby-forum.com/t/is-it-possible-for-table-intersection-reference-same-table/144668 "2008-08-01T01:42:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![betame342](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/90db22/32.png) [@betame342](https://www.ruby-forum.com/u/betame342)\
**Post date:** [August 1, 2008, 1:42am UTC](https://www.ruby-forum.com/t/is-it-possible-for-table-intersection-reference-same-table/144668/1 "2008-08-01T01:42:31Z")

</div>

Hi,

## Hopefully this is not confusing. ---- WHAT I need is this below ---------------- SQL Statement Select \* from table\_mains left join table\_a where table\_a.id = table\_mains.table\_a\_id left join table\_b where table\_b.id = table\_mains.table\_b\_id left join photo pa where pa.id = table\_mains.photo\_a\_id left join photo pb where pb.id = table\_mains.photo\_b\_id

What I have try  
table\_mains = TableMain.find(:all,:include=\>[:table\_a, :table\_b,  
:photo])

It only return left join from table\_b but not table\_a

--------Here are the tables--------------------  
table\_mains

- table\_a\_id (column)
- photo\_a\_id (column)
- table\_b\_id (column)
- photo\_b\_id (column)

## photos table\_a table\_b

## class TableMain \< ActiveRecord::Base belongs\_to :table\_a,:foreign\_key =\> “table\_a\_id” belongs\_to :table\_b,:foreign\_key =\> “table\_b\_id” belongs\_to :photo,:foreign\_key =\> “photo\_a\_id” belongs\_to :photo,:foreign\_key =\> “photo\_b\_id” end

## class TableA \< ActiveRecord::Base has\_one :table\_main end

## class TableB \< ActiveRecord::Base has\_one :table\_main end

class Photo \< ActiveRecord::Base  
has\_one :table\_main  
end

---

<div class="post-metadata">

**Author:** ![betame342](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/90db22/32.png) [@betame342](https://www.ruby-forum.com/u/betame342)\
**Post date:** [August 1, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/is-it-possible-for-table-intersection-reference-same-table/144668/2 "2008-08-01T20:04:23Z")

</div>

Just in case, if any one else interested.

I resolved the problem by using class\_name in has\_many
