# Polymorphic comments table needs associated model

**URL:** <https://www.ruby-forum.com/t/polymorphic-comments-table-needs-associated-model/166748>\
**Category:** Rails\
**Created:** [April 29, 2009, 7:00am UTC](https://www.ruby-forum.com/t/polymorphic-comments-table-needs-associated-model/166748 "2009-04-29T07:00:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [April 29, 2009, 7:00am UTC](https://www.ruby-forum.com/t/polymorphic-comments-table-needs-associated-model/166748/1 "2009-04-29T07:00:00Z")

</div>

Howdy,

I’ve been banging my head on this problem for a bit.

I’m using the acts\_as\_commentable plugin which works great.

The scenario, I’m on the user’s dashboard and want to display comments  
with commentable\_type = Venue. No problem. But, if I want to display  
the name of that Venue with the comment… big nasty. The polymorphic  
key is a composite key and rails doesn’t have a way to recognize  
this. I really want to keep with Active Record so I tried  
has\_many\_polymorphs, which makes finding the venue through the  
comments table possible but it doesn’t JOIN the venue table with the  
comments table. So I resorted to writing a sql statement.

#User.rb

acts\_as\_commentable  
has\_many\_polymorphs :commentables, :from =\>  
[:venues, :artists, :events], :through =\> :comments

# –\> WORKS but doesn’t include the venue

@user.comments.find :all, :conditions =\> “`comments`.commentable\_type  
= ‘Venue’”

# –\> Doesn’t work, but it’s my intention

@user.comments.find :all, :conditions =\> “`comments`.commentable\_type  
= ‘Venue’”, :include =\> :venues

#–\> WORKS but it’s ugly

Comment.find\_by\_sql “SELECT `comments`. \* , `venues`. name FROM  
`comments` INNER JOIN `venues` ON venues.id = comments.commentable\_id  
WHERE (user\_id =#{self.id} AND commentable\_type = ‘Venue’) ORDER BY  
created\_at DESC LIMIT 0 , 30”

Does anyone have any ideas on how I can avoid using find\_by\_sql for  
this operation?

Best,

Jackson
