# Has\_many :through with self-referencial AND polymorphism

**URL:** <https://www.ruby-forum.com/t/has-many-through-with-self-referencial-and-polymorphism/126487>\
**Category:** Rails\
**Created:** [January 18, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/has-many-through-with-self-referencial-and-polymorphism/126487 "2008-01-18T17:33:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [January 18, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/has-many-through-with-self-referencial-and-polymorphism/126487/1 "2008-01-18T17:33:38Z")

</div>

Hello, despite many readings I still don’t understand how to solve my  
problem.

I have Publication, Picture, File classes  
A Publication can be related (a one way relation, like a www link) with  
another Publication, Picture or File

I tried this :

Publication :  
has\_many :related\_elements, :dependent =\> :destroy, :foreign\_key =\>  
:publication\_id  
has\_many :related, :through =\> :related\_elements, :as =\> :linked,  
:source =\> :publication

RelatedElement :  
belongs\_to :publication

And migrate as  
create\_table :related\_elements do |t|  
t.integer :linked\_id  
t.string :linked\_type  
t.integer :publication\_id  
end

When I do  
a\_publication.related[0] = another\_publication  
a\_publication.save!

I would expect the related\_elements table beeing updated with  
publication\_id = the id of a\_publication  
linked\_id = the id of another\_publication  
linked\_type = the class name ‘Publication’

But nothing happen… And I must confess that I’m pretty sure my  
has\_many are totally wrong

All this stuff is really too complex for me, so your help will be  
greatly appreciated !! Thanks

---

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [January 18, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/has-many-through-with-self-referencial-and-polymorphism/126487/2 "2008-01-18T23:46:41Z")

</div>

here you have an example

[http://pastie.org/140772](http://pastie.org/140772)

I hope it helps.

2008/1/18, Tom S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Publication :  
> t.integer :linked\_id  
> linked\_id = the id of another\_publication
> 
> >

## –

Pedro Del G.

Email : removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [January 21, 2008, 5:34pm UTC](https://www.ruby-forum.com/t/has-many-through-with-self-referencial-and-polymorphism/126487/3 "2008-01-21T17:34:23Z")

</div>

Pedro Del gallego wrote:

Thanks Pedro, this is a first step in the right direction.  
Now I would like to add support for polymorphism, I mean that the item  
related could be of different class (this → related\_to → book, this →   
related\_to → image, and so on)

It seems that it’s not a simple case  
([has\_many :through - The other side of polymorphic :through associations](http://blog.hasmanythrough.com/2006/4/3/polymorphic-through))

Does it changed with Rails 2.0 ?

I tried the solution provided on the hasmanythrough blog but without  
success.

Should I go to the has\_many\_polymorphs plugin ??  
([http://blog.evanweaver.com/files/doc/fauna/has\_many\_polymorphs/files/README.html](http://blog.evanweaver.com/files/doc/fauna/has_many_polymorphs/files/README.html))

I don’t like the idea of overusing plugins when I don’t know how to  
handle a special situation… It’s kind of black box in that case

Thanks
