# Rails Polymorphic conundrum

**URL:** <https://www.ruby-forum.com/t/rails-polymorphic-conundrum/175598>\
**Category:** Rails\
**Created:** [September 16, 2009, 9:54am UTC](https://www.ruby-forum.com/t/rails-polymorphic-conundrum/175598 "2009-09-16T09:54:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thelemcke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thelemcke](https://www.ruby-forum.com/u/thelemcke)\
**Post date:** [September 16, 2009, 9:54am UTC](https://www.ruby-forum.com/t/rails-polymorphic-conundrum/175598/1 "2009-09-16T09:54:11Z")

</div>

I’m making a database that requires many-to-many associations. Here’s  
the design:

There is a class called ‘Owner’ that owns many ‘Boxes’, ‘Things’,  
‘Doodads’, and ‘Thingamabobs’

‘Boxes’ can have many ‘Things’, ‘Doodads’, and ‘Thingamabobs’, and  
each ‘Thing’, ‘Doodad’, or ‘Thingamabob’ can belong to many :boxes. Do  
I just need to make three separate join tables, or is there any way to  
create this with a polymorphic association like so:

BoxesContents  
box\_id  
content\_type  
content\_id

(content being a :thing, :doodad, or :thingamabob

---

<div class="post-metadata">

**Author:** ![thelemcke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thelemcke](https://www.ruby-forum.com/u/thelemcke)\
**Post date:** [September 16, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/rails-polymorphic-conundrum/175598/2 "2009-09-16T12:07:22Z")

</div>

I’m pretty confident you can’t do polymorphic with HABTM. You can with a  
one-to-many (has\_many, belongs\_to) but not with HABTM.

steve

---

<div class="post-metadata">

**Author:** ![thelemcke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thelemcke](https://www.ruby-forum.com/u/thelemcke)\
**Post date:** [September 17, 2009, 1:31am UTC](https://www.ruby-forum.com/t/rails-polymorphic-conundrum/175598/3 "2009-09-17T01:31:25Z")

</div>

Is it possible to do this without the use of has\_and\_belongs\_to\_many?

On Sep 16, 3:07 am, Steve R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![thelemcke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thelemcke](https://www.ruby-forum.com/u/thelemcke)\
**Post date:** [September 17, 2009, 1:38am UTC](https://www.ruby-forum.com/t/rails-polymorphic-conundrum/175598/4 "2009-09-17T01:38:20Z")

</div>

theLemcke wrote:

> Is it possible to do this without the use of has\_and\_belongs\_to\_many?

Without looking at your case in detail, let me remind you that any habtm  
can also be represented by an equivalent has\_many :through.

> On Sep 16, 3:07ï¿½am, Steve R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
