# Has\_many :through with a polymorphic join

**URL:** <https://www.ruby-forum.com/t/has-many-through-with-a-polymorphic-join/61197>\
**Category:** Rails\
**Created:** [May 21, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/has-many-through-with-a-polymorphic-join/61197 "2006-05-21T14:15:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 21, 2006, 2:15pm UTC](https://www.ruby-forum.com/t/has-many-through-with-a-polymorphic-join/61197/1 "2006-05-21T14:15:14Z")

</div>

Hi,  
Four tables: users, user\_counties, uk\_counties and us\_counties.

Each user has many counties, and each county has many users, so I  
decided to make user\_counties a polymorph, so it can have counties  
from different countries (each country requires a completely different  
set of tables with a completely different set of properties, that’s  
why there’s one table for uk\_counties and one for us\_counties, with  
more to follow when more countries are supported). This would require:

class User…  
has\_many :counties, :through =\> user\_counties #counties is the  
imaginary table  
end

class UserCounties  
belongs\_to :counties, :polymorphic =\> true  
end

Is this possible? If not, can anyone think of a way around it other  
than adding another table into the chain? And (bonus question… not  
required, just for fun) what would the reverse relationship look like  
(ie, how would uk\_counties relate to users)?  
-Nathan

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 22, 2006, 2:44am UTC](https://www.ruby-forum.com/t/has-many-through-with-a-polymorphic-join/61197/2 "2006-05-22T02:44:40Z")

</div>

unknown wrote:

> Four tables: users, user\_counties, uk\_counties and us\_counties.
> 
> Each user has many counties, and each county has many users, so I  
> decided to make user\_counties a polymorph…

[http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through](http://blog.hasmanythrough.com/articles/2006/04/03/polymorphic-through)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 22, 2006, 4:49am UTC](https://www.ruby-forum.com/t/has-many-through-with-a-polymorphic-join/61197/3 "2006-05-22T04:49:07Z")

</div>

Ah, thanks Josh. Nice to see that you’d already written an in-depth  
answer to my problem.  
-N

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 22, 2006, 6:57am UTC](https://www.ruby-forum.com/t/has-many-through-with-a-polymorphic-join/61197/4 "2006-05-22T06:57:40Z")

</div>

unknown wrote:

> Ah, thanks Josh. Nice to see that you’d already written an in-depth  
> answer to my problem.

About half the reason I keep the blog is to avoid having to repeat  
myself 🙂  
Glad you found it useful!

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
