# Polymorphic Associations with inheritance

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations-with-inheritance/152056>\
**Category:** Rails\
**Created:** [October 29, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-with-inheritance/152056 "2008-10-29T15:31:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [October 29, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-with-inheritance/152056/1 "2008-10-29T15:31:42Z")

</div>

I have two models, Article and Post, that has many Comments. However,  
both Article and Post are inherited from a BaseContent class like so:

class Article \< BaseContent  
has\_many :comments, :as =\> :commentable  
end

class Post \< BaseContent  
has\_many :comments, :as =\> :commentable  
end

and here’s my Comment model:

class Comment \< ActiveRecord::Base  
belongs\_to :commentable, :polymorphic =\> true  
end

Now when I try to associate a comment with an Article or Post, the  
commentable\_type field gets set as “BaseContent” instead of “Article”  
or “Post.” Is there a way to specify what the commentable\_type field  
is?

---

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [October 29, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-with-inheritance/152056/2 "2008-10-29T18:36:33Z")

</div>

On Oct 29, 3:31 pm, et [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and here’s my Comment model:
> 
> class Comment \< ActiveRecord::Base  
> belongs\_to :commentable, :polymorphic =\> true  
> end
> 
> Now when I try to associate a comment with an Article or Post, the  
> commentable\_type field gets set as “BaseContent” instead of “Article”  
> or “Post.” Is there a way to specify what the commentable\_type field  
> is?

Hi,

can you show how are you creating Post and Comment and “joining” them?

---

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [October 29, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-with-inheritance/152056/3 "2008-10-29T18:54:09Z")

</div>

I’m just doing:

comment = Comment.New()

# set comment properties here

comment.save  
Post.comments \<\< comment

---

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [October 29, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-with-inheritance/152056/4 "2008-10-29T19:51:37Z")

</div>

That still saves the comment as commentable\_id = 1, commentable\_type =  
BaseContent.

So when I save a comment for Article with id 1 and another comment for  
Post with id 1, there now are two comments with commentable\_id = 1,  
commentable\_type = BaseContent and they both show up with I do  
Article.find(1).comments and Post.find(1).comments

---

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [October 29, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-with-inheritance/152056/5 "2008-10-29T19:22:19Z")

</div>

Have you tried this way?

It worked for me:

comment = Comment.New()  
comment.commentable = post #instance of the actual post you are  
commenting

# set comment properties here

comment.save

---

<div class="post-metadata">

**Author:** ![etetet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@etetet](https://www.ruby-forum.com/u/etetet)\
**Post date:** [October 29, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-with-inheritance/152056/6 "2008-10-29T20:41:06Z")

</div>

I forgot about the problems with STI  
you would have to define commentable\_type method in BaseContent as  
said in the: activerecord/lib/active\_record/associations.rb

```
# Using polymorphic associations in combination with single table

```

inheritance (STI) is a little tricky. In order  
# for the associations to work as expected, ensure that you store  
the base model for the STI models in the  
# type column of the polymorphic association. To continue with the  
asset example above, suppose there are guest posts  
# and member posts that use the posts table for STI. In this case,  
there must be a +type+ column in the posts table.  
#  
# class Asset \< ActiveRecord::Base  
# belongs\_to :attachable, :polymorphic =\> true  
#  
# def attachable\_type=(sType)  
# super(sType.to\_s.classify.constantize.base\_class.to\_s)  
# end  
# end  
#  
# class Post \< ActiveRecord::Base  
# # because we store “Post” in attachable\_type now :dependent  
=\> :destroy will work  
# has\_many :assets, :as =\> :attachable, :dependent =\> :destroy  
# end  
#  
# class GuestPost \< Post  
# end  
#  
# class MemberPost \< Post  
# end  
#
