Polymorphic comments table needs associated model


#1

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