# Polymorphic Association?

**URL:** <https://discuss.rubyonrails.org/t/polymorphic-association/19446>\
**Category:** rubyonrails-talk\
**Created:** [October 18, 2007, 11:51pm UTC](https://discuss.rubyonrails.org/t/polymorphic-association/19446 "2007-10-18T23:51:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Davis](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@Jeff\_Davis](https://discuss.rubyonrails.org/u/Jeff_Davis)\
**Post date:** [October 18, 2007, 11:51pm UTC](https://discuss.rubyonrails.org/t/polymorphic-association/19446/1 "2007-10-18T23:51:40Z")

</div>

Hi there-

I'm a newbie to this board, RoR, and web development in general. So please forgive me if this is in the wrong place or if the solution is really simple...

I'm trying to conceptualize the relationships between a series of models for a page creation function. What I'd like is to have a Content Page which consists of a series of Content Blocks. The Content Blocks act as a list relative to the Content Page. And each Content Block can be assigned one of 3 types of content: TextContent, ImageContent, or FileContent. So my current thinking is this:

ContentPage &nbsp;&nbsp;has\_many :content\_blocks, :order =\> :position

ContentBlock &nbsp;&nbsp;belongs\_to :content\_page &nbsp;&nbsp;acts\_as\_list :scope =\> content\_page\_id &nbsp;&nbsp;has\_one :text\_content &nbsp;&nbsp;has\_one :image\_content &nbsp;&nbsp;has\_one :file\_content

TextContent &nbsp;&nbsp;belongs\_to :content\_block

ImageContent &nbsp;&nbsp;belongs\_to :content\_block

FileContent &nbsp;&nbsp;belongs\_to :content\_block

But when I envision looking up a Content Page and its associated Content Blocks, I feel like it will be difficult to keep going and find the actual Text, Image, or File data for each Content Block. I researched polymorphic associations a little bit, but it seems like that would be a backwards way to approach this, with the Content Blocks having to belong to the Text, Image, and File Contents.

Anyway, I'm having a really hard time conceptualizing this for some reason and any help would be greatly appreciated!

Thanks,

Jeff

---

<div class="post-metadata">

**Author:** ![Craig\_White1](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@Craig\_White1](https://discuss.rubyonrails.org/u/Craig_White1)\
**Post date:** [October 18, 2007, 11:59pm UTC](https://discuss.rubyonrails.org/t/polymorphic-association/19446/2 "2007-10-18T23:59:16Z")

</div>

> Hi there-
> 
> I'm a newbie to this board, RoR, and web development in general. So please forgive me if this is in the wrong place or if the solution is really simple...
> 
> I'm trying to conceptualize the relationships between a series of models for a page creation function. What I'd like is to have a Content Page which consists of a series of Content Blocks. The Content Blocks act as a list relative to the Content Page. And each Content Block can be assigned one of 3 types of content: TextContent, ImageContent, or FileContent. So my current thinking is this:
> 
> ContentPage &nbsp;&nbsp;has\_many :content\_blocks, :order =\> :position
> 
> ContentBlock &nbsp;&nbsp;belongs\_to :content\_page &nbsp;&nbsp;acts\_as\_list :scope =\> content\_page\_id &nbsp;&nbsp;has\_one :text\_content &nbsp;&nbsp;has\_one :image\_content &nbsp;&nbsp;has\_one :file\_content
> 
> TextContent &nbsp;&nbsp;belongs\_to :content\_block
> 
> ImageContent &nbsp;&nbsp;belongs\_to :content\_block
> 
> FileContent &nbsp;&nbsp;belongs\_to :content\_block
> 
> But when I envision looking up a Content Page and its associated Content Blocks, I feel like it will be difficult to keep going and find the actual Text, Image, or File data for each Content Block. I researched polymorphic associations a little bit, but it seems like that would be a backwards way to approach this, with the Content Blocks having to belong to the Text, Image, and File Contents.
> 
> Anyway, I'm having a really hard time conceptualizing this for some reason and any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![Jeff\_Davis](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@Jeff\_Davis](https://discuss.rubyonrails.org/u/Jeff_Davis)\
**Post date:** [October 19, 2007, 12:03am UTC](https://discuss.rubyonrails.org/t/polymorphic-association/19446/3 "2007-10-19T00:03:03Z")

</div>

Yes, it would.

---

<div class="post-metadata">

**Author:** ![Bob\_Showalter](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@Bob\_Showalter](https://discuss.rubyonrails.org/u/Bob_Showalter)\
**Post date:** [October 19, 2007, 2:05am UTC](https://discuss.rubyonrails.org/t/polymorphic-association/19446/4 "2007-10-19T02:05:36Z")

</div>

Another way to approach it would be with STI (single table inheritance). You create a Content model (and contents table) and then subclass it to form your specific content classes:

&nbsp;&nbsp;&nbsp;class ContentBlock \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_one :content &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class Content \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;belongs\_to :content\_block &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class TextContent \< Content &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class FileContent \< Content &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class ImageContent \< Content &nbsp;&nbsp;&nbsp;end

You need a "type" column (and a content\_block\_id of course) in your contents table.

The nice thing is that content\_block.content is polymorphic; it will be a TextContent, FileContent, or ImageContent object, depending on what you've assigned to it. You can also put common methods in the Content class to be shared across all the specific classes.

The downside is that there is only one physical table, so all the attributes have to go there.

---

<div class="post-metadata">

**Author:** ![Jeff\_Davis](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@Jeff\_Davis](https://discuss.rubyonrails.org/u/Jeff_Davis)\
**Post date:** [October 19, 2007, 4:25am UTC](https://discuss.rubyonrails.org/t/polymorphic-association/19446/5 "2007-10-19T04:25:58Z")

</div>

Thanks Bob, I appreciate the response. If I went this direction, would I be able to bypass the Content model altogether and just have TextContent, etc. be subclasses of the ContentBlock model?

I guess I'm also interested in how to do this in separate tables as well, since TextContent, ImageContent, and FileContent will all hold differing types of information.
