# Rails 3: many to many polymorphic relationship

**URL:** <https://www.ruby-forum.com/t/rails-3-many-to-many-polymorphic-relationship/194272>\
**Category:** Rails\
**Created:** [September 1, 2010, 10:27pm UTC](https://www.ruby-forum.com/t/rails-3-many-to-many-polymorphic-relationship/194272 "2010-09-01T22:27:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [September 1, 2010, 10:27pm UTC](https://www.ruby-forum.com/t/rails-3-many-to-many-polymorphic-relationship/194272/1 "2010-09-01T22:27:50Z")

</div>

Hello,

I’ve been googling for about two hours already but not successful yet so  
I hope you guys can help me out.

I need some kind of n:m polymorphic relation like this:  
building\_types: living, shops  
block\_types: residential, commercial

activity\_types: act1, act2

So now I need a connection table e.g. activity\_scope so I can for  
example allow  
act1 only for living and residential and act2 for commercial and shops

Any ideas?

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [September 1, 2010, 11:51pm UTC](https://www.ruby-forum.com/t/rails-3-many-to-many-polymorphic-relationship/194272/2 "2010-09-01T23:51:34Z")

</div>

Hi Heinz,

can you explain a bit more what is the goal? I’m not an expert, so maybe  
I  
misunderstood.

What is wrong with ‘has\_many’ and ‘belongs\_to’ models? If a record can  
have  
many types, which defined in other database, then it can be explicitly  
defined in the model which it belongs to with these.  
(I’m not sure if you thought about this.)

I think check Active Record, and how to use it, your answer will be  
there  
for sure! (Agile Web D. with Rails have 3 deep chapter about it  
;))

Let me know how is your problem going,  
all the bests,  
gezope

2010/9/1 Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [September 2, 2010, 5:42am UTC](https://www.ruby-forum.com/t/rails-3-many-to-many-polymorphic-relationship/194272/3 "2010-09-02T05:42:46Z")

</div>

Heinz S. wrote:

> Hello,
> 
> I’ve been googling for about two hours already but not successful yet so  
> I hope you guys can help me out.
> 
> I need some kind of n:m polymorphic relation like this:  
> building\_types: living, shops  
> block\_types: residential, commercial
> 
> activity\_types: act1, act2
> 
> So now I need a connection table e.g. activity\_scope so I can for  
> example allow  
> act1 only for living and residential and act2 for commercial and shops
> 
> Any ideas?

This doesn’t sound like a polymorphic relationship (at least not in  
terms of tables). Are living, shop, residential and commercial each  
models, or just attribute values for a building model (living or shop)  
and a block model (residential or commercial)?

It feels like this is more just a case of a complex validation where you  
need to check the building type of a related building and the block\_type  
of a related block according to the activity type.
