# How to achieve STI has\_many :through referencing to the same STI

**URL:** <https://www.ruby-forum.com/t/how-to-achieve-sti-has-many-through-referencing-to-the-same-sti/159978>\
**Category:** Rails\
**Created:** [February 10, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/how-to-achieve-sti-has-many-through-referencing-to-the-same-sti/159978 "2009-02-10T17:03:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![satynos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@satynos](https://www.ruby-forum.com/u/satynos)\
**Post date:** [February 10, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/how-to-achieve-sti-has-many-through-referencing-to-the-same-sti/159978/1 "2009-02-10T17:03:31Z")

</div>

I have the base Content model from which all the types of the contents  
such as page, post, asset descend as follows:

class Content \< ActiveRecord::Base  
end

class Page \< Content  
end

class Post \< Content  
end

class Asset \< Content  
end

Now I am trying to accomplish relationships between all these contents  
using a relationship table as follows:

class Relationship \< ActiveRecord::Base  
belongs\_to :content, :polymorphic =\> true  
belongs\_to :target, :polymorphic =\> true  
end

Accordingly I updated the base models to reflect the relationships as  
follows:

class Page \< Content  
has\_many :relationships, :as =\> :content  
has\_many :posts, :through =\> :relationships, :source  
=\> :target, :source\_type =\> “Post”  
has\_many :assets, :through =\> :relationships, :source  
=\> :target, :source\_type =\> “Asset”  
end

class Post \< Content  
has\_many :relationships, :as =\> :content  
has\_many :pages, :through =\> :relationships, :source  
=\> :target, :source\_type =\> “Page”  
has\_many :assets, :through =\> :relationships, :source  
=\> :target, :source\_type =\> “Asset”  
end

class Asset \< Content  
has\_many :relationships, :as =\> :content  
has\_many :pages, :through =\> :relationships, :source  
=\> :target, :source\_type =\> “Page”  
has\_many :posts, :through =\> :relationships, :source  
=\> :target, :source\_type =\> “Post”  
end

But when I try to access @page.assets, I am getting empty array. In  
looking at the log file, I see the reason being the following query:

SELECT `contents`.\* FROM `contents`  
INNER JOIN relationships ON contents.id = relationships.target\_id  
**AND relationships.target\_type = ‘Asset’**  
WHERE (`relationships`.content\_id = #{id} ) AND (`contents`.`type` =  
‘Asset’)

The problem is due to the highlighted portion of the query. When the  
active record saves a relationship, it is saving the content\_type and  
target\_type as “Content” (the base model class) instead of the actual  
content type “Page” or “Post” or “Asset”.

Is there any way possible to achieve what I am trying without any  
plugins? How can I tell ActiveRecord to save the content\_type and  
target\_type of the relationship with the actual class name instead of  
the base class name?

BTW: I looked into has\_many\_polymorphs, but it doesn’t quite solve my  
problem.

Thanks in advance.  
-Satynos
