ActiveRecord - querying polymorphic associations?

The accepted solution does not work once you introduce another model that has an association using "commentable". Commentable_id is not unique and therefore you'll start retrieving the wrong comments For example: You decide to add a news model that accepts comments class News :commentable end Now you may get two records back if you made a comment on a forum_topic with an id of 1 and a news article with an id of 1 using your query: :joins => "INNER JOIN forum_topics ON forum_topics. Id = comments.

Commentable_id You could probably solve the problem by supplying a commentable_type as one of your conditions, but I don't think that's the best way to approach this issue.

The accepted solution does not work once you introduce another model that has an association using "commentable". Commentable_id is not unique and therefore you'll start retrieving the wrong comments. For example: You decide to add a news model that accepts comments... class News :commentable end Now you may get two records back if you made a comment on a forum_topic with an id of 1 and a news article with an id of 1 using your query: :joins => "INNER JOIN forum_topics ON forum_topics.

Id = comments. Commentable_id" You could probably solve the problem by supplying a commentable_type as one of your conditions, but I don't think that's the best way to approach this issue.

Actually I think you're suggestion of supplying a commentable_type as one of the conditions is the right way to go... the act_as_taggable_on plugin does exactly that with their taggings table containing (amongst others) the columns: tag_id, taggable_id and taggable_type – AJP Aug 23 at 1:43.

Argh! I think I found the problem. When joining via: @comments = Comment.

Find(:all, :joins => "forum_topics", :conditions => "forum_topics. Featured =? ", true ) You need the whole join!

:joins => "INNER JOIN forum_topics ON forum_topics. Id = comments. Commentable_id", See the ever-awesome: guides.rubyonrails.org/active_record_que....

– Toby Hede Mar 25 '09 at 4:07 1 At least you posted the solution. Kudos. – srboisvert Mar 25 '09 at 13:00 1 I think you also need to check that comments.

Commentable_type is the right type (in your case "ForumTopic") otherwise you are doing a join with ALL the comments. Commentable_id entries matching forum_topics. Id even when they aren't forum topics.

– Jits Jul 12 at 10:07.

I am using polymorphic associations to track Comments in my project. All very straight forward stuff. The problem I have is in querying based on the polymorphic association and joinging from the Comment model back to it's owner.

I have several other "commentable" models that aren't important right now. All of this works.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions