# Multi-table join issue with Legacy tables

**URL:** <https://www.ruby-forum.com/t/multi-table-join-issue-with-legacy-tables/114742>\
**Category:** Rails\
**Created:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/multi-table-join-issue-with-legacy-tables/114742 "2007-09-17T13:25:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nshanny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9a140/32.png) [@nshanny](https://www.ruby-forum.com/u/nshanny)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/multi-table-join-issue-with-legacy-tables/114742/1 "2007-09-17T13:25:01Z")

</div>

Hi there,

I have been banging my head against the wall and need to reach out and  
ask for help.

I have three tables and thus three models, Cars, entity\_map, and  
wiki\_edits.

Cars has a primary key named id  
entity\_map has two columns: ref\_id and entity\_id  
wiki\_edits has two columns: id (PK) and entity\_id

to find a car’s wiki\_edits I would issue the following SQL fragment  
join

from cars join entity\_map on (cars.id = entity\_map.ref\_id) join  
wiki\_edits on (entity\_map.entity\_id = wiki\_edits.entity\_id)

The schema is set so I cannot change it. How would I use a combination  
of has\_many, belongs\_to, and through attributes to define this  
relationship inside the models? I have tried a bunch of different  
approaches and have thus failed.

Any insight would be great.

Thanks in advance.  
Nick Shanny
