# Polymorphic Association?

**URL:** <https://www.ruby-forum.com/t/polymorphic-association/117667>\
**Category:** Rails\
**Created:** [October 19, 2007, 1:52am UTC](https://www.ruby-forum.com/t/polymorphic-association/117667 "2007-10-19T01:52:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2007, 1:52am UTC](https://www.ruby-forum.com/t/polymorphic-association/117667/1 "2007-10-19T01:52:20Z")

</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  
has\_many :content\_blocks, :order =\> :position

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

TextContent  
belongs\_to :content\_block

ImageContent  
belongs\_to :content\_block

FileContent  
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:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2007, 1:59am UTC](https://www.ruby-forum.com/t/polymorphic-association/117667/2 "2007-10-19T01:59:56Z")

</div>

On Thu, 2007-10-18 at 16:51 -0700, removed\_email\_address@domain.invalid wrote:

> Content Block can be assigned one of 3 types of content: TextContent,  
> has\_one :file\_content  
> 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!

* * *

would a single specific TextContent belong to 1 and only 1 ContentBlock?

–  
Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2007, 4:38am UTC](https://www.ruby-forum.com/t/polymorphic-association/117667/3 "2007-10-19T04:38:28Z")

</div>

Yes, it would.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2007, 4:38am UTC](https://www.ruby-forum.com/t/polymorphic-association/117667/4 "2007-10-19T04:38:29Z")

</div>

On 10/18/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Content Blocks act as a list relative to the Content Page. And each  
> has\_one :image\_content
> 
> 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!

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:

class ContentBlock \< ActiveRecord::Base  
has\_one :content  
end

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

class TextContent \< Content  
end

class FileContent \< Content  
end

class ImageContent \< Content  
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:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 19, 2007, 6:26am UTC](https://www.ruby-forum.com/t/polymorphic-association/117667/5 "2007-10-19T06:26:37Z")

</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.
