# Polymorphic :through relationship

**URL:** <https://www.ruby-forum.com/t/polymorphic-through-relationship/109133>\
**Category:** Rails\
**Created:** [July 11, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/polymorphic-through-relationship/109133 "2007-07-11T19:22:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![michau](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@michau](https://www.ruby-forum.com/u/michau)\
**Post date:** [July 11, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/polymorphic-through-relationship/109133/1 "2007-07-11T19:22:07Z")

</div>

Is it possible to implement a model where:

Exit is polymorphic and can be of type: Door, Window, FootPath

and

Rooms are related through Exits with eachother.

I figured out that I would implement polymorphic exits, and on top of  
that I will use the through model, so the models’ definitions look  
like:

class Room  
has\_many :exits,  
has\_many :rooms, through :exits

class Exit  
belongs\_to :resource, :polymorphic =\> true  
belongs\_to :room

class Door  
has\_one :exit\_entry, :as =\> :resource

class Window  
has\_one :exit\_entry, :as =\> :resource

class FootPath  
has\_one :exit\_entry, :as =\> :resource

Also Im not sure what foreign key refering to Room should be in Exit?  
I quess I should have something like:  
origin\_room\_id  
goto\_room\_id  
but on the other hand the :through model requires, that there is  
room\_id

So overall any help on all that anyone?
