# Has\_many through join table - join three tables?

**URL:** <https://www.ruby-forum.com/t/has-many-through-join-table-join-three-tables/172814>\
**Category:** Rails\
**Created:** [July 28, 2009, 1:43pm UTC](https://www.ruby-forum.com/t/has-many-through-join-table-join-three-tables/172814 "2009-07-28T13:43:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [July 28, 2009, 1:43pm UTC](https://www.ruby-forum.com/t/has-many-through-join-table-join-three-tables/172814/1 "2009-07-28T13:43:49Z")

</div>

Hi

Is it feasible or horribly wrong to include a boolean column to a join  
table in order to ‘junction’ a join?

Imagine you want to map a journey that will comprise multiple flights  
and train journeys.

# train.rb

# flight.rb

# stages.rb

# itinerary.rb

Itinerary has many flights or trains journeys through stages.

I could put a column for each id, train and flight, or I can put a  
boolean flag that dictates whether the stage is a flight or train  
journey and define in the model the foreign key in both cases. In the  
case of the former, there will be a lot of null values so the latter  
seems preferable. Maybe an answer is a further join table, but for the  
purpose of this that is really not ideal.

So will this work ok or am I barking up the wrong tree? Thanks in  
advance. 🙂

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [July 28, 2009, 1:52pm UTC](https://www.ruby-forum.com/t/has-many-through-join-table-join-three-tables/172814/2 "2009-07-28T13:52:01Z")

</div>

2009/7/28 Mark D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> # flight.rb
> 
> purpose of this that is really not ideal.
> 
> So will this work ok or am I barking up the wrong tree? Thanks in  
> advance. 🙂

Have you considered using Single Table Inheritance so that train and  
flight are two types of stages? Then an itinerary has many stages,  
each of which is a flight or a train, or boat or whatever.

Colin

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [July 28, 2009, 2:07pm UTC](https://www.ruby-forum.com/t/has-many-through-join-table-join-three-tables/172814/3 "2009-07-28T14:07:38Z")

</div>

Hi Colin. Unfortunately in this case sti isn’t feasible. Sorry, I should  
have pointed out that the scenario provided is a simplified  
demonstration.

It occurs to me that this is a polymorphic has\_many\_through which led me  
to this article:

[http://blog.hasmanythrough.com/2006/4/3/polymorphic-through](http://blog.hasmanythrough.com/2006/4/3/polymorphic-through)

It’s a few years old but seems applicable.

Advice and experiences still very welcome. 🙂
