# Multiple polymorphism?

**URL:** <https://www.ruby-forum.com/t/multiple-polymorphism/155124>\
**Category:** Rails\
**Created:** [December 8, 2008, 12:39pm UTC](https://www.ruby-forum.com/t/multiple-polymorphism/155124 "2008-12-08T12:39:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincenzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincenzo](https://www.ruby-forum.com/u/vincenzo)\
**Post date:** [December 8, 2008, 12:39pm UTC](https://www.ruby-forum.com/t/multiple-polymorphism/155124/1 "2008-12-08T12:39:41Z")

</div>

Hello all,

Let me explain a fairly complex problem, I am blocked  
and if someone could give me a boost I can thank him  
enough!

## The concept:

I have a series of tables in DB (users, functions, expertize, …)

I want to put in place a system that would allow me to link these  
different objects between them in a very flexible one  
table.

## My idea:

Create a table “objectrelations” containing the following fields:

id  
linkfrom\_id  
linkfrom\_type  
linkto\_id  
linkto\_type  
created\_at  
updated\_at  
stopped\_at

# 1 record objectrelations:

id: 1  
linkfrom\_id: 1 # User ID = 1  
linkfrom\_type: “User”  
linkto\_id: 1 # expertize ID = 1  
linkto\_type: “expertize”  
created\_at: now ()  
updated\_at: now ()  
stopped\_at: NULL

## The problem:

This works well in DB, it allows me to create links between  
different objects, back links, to end a relationship  
(via the column stopped\_at) without removing the link DB (it keeps a  
history) …

As against, I don’t find how to implement this model in  
Rails (in models).

I think I understand that I will use the polymorphism,  
the “Through Associations” or “Single table inheritance” but I don’t  
know how.

I looked at the side of “acts\_as\_taggable”, they use a system  
similar but only in one direction (any object -\>  
tags). Unfortunately, here I must be able to link anything with  
anything …

Does anyone have an idea to help me? How should I do this ?

Thank you

---

<div class="post-metadata">

**Author:** ![vincenzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincenzo](https://www.ruby-forum.com/u/vincenzo)\
**Post date:** [December 8, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/multiple-polymorphism/155124/2 "2008-12-08T15:26:03Z")

</div>

Maybe I’m missing something, but it doesn’t sound like you need  
acts\_as\_taggable or single table inheritance. I think you will get the  
desired result by setting up the ObjectRelations model as follows:

class ObjectRelations \< ActiveRecord::Base  
belongs\_to :linkfrom, :polymorphic =\> true  
belongs\_to :linkto, :polymorphic =\> true  
end

class User \< ActiveRecord::Base  
has\_many :objectrelations, :as =\> :linkto  
has\_many :objectrelations, :as =\> :linkfrom  
end

On Dec 8, 5:39 am, Vincenzo Ruggiero \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![vincenzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vincenzo](https://www.ruby-forum.com/u/vincenzo)\
**Post date:** [December 8, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/multiple-polymorphism/155124/3 "2008-12-08T15:30:49Z")

</div>

Andrew B. wrote:

> Maybe I’m missing something, but it doesn’t sound like you need  
> acts\_as\_taggable or single table inheritance. I think you will get the  
> desired result by setting up the ObjectRelations model as follows:
> 
> class ObjectRelations \< ActiveRecord::Base  
> belongs\_to :linkfrom, :polymorphic =\> true  
> belongs\_to :linkto, :polymorphic =\> true  
> end
> 
> class User \< ActiveRecord::Base  
> has\_many :objectrelations, :as =\> :linkto  
> has\_many :objectrelations, :as =\> :linkfrom  
> end
> 
> On Dec 8, 5:39ï¿½am, Vincenzo Ruggiero \<rails-mailing-l…@andreas-

Agreed…

[http://wiki.rubyonrails.org/rails/pages/ManytoManyPolymorphicAssociations](http://wiki.rubyonrails.org/rails/pages/ManytoManyPolymorphicAssociations)
