Selecting a single entry in a connection request in rails3

One article has many comments and I want to get all articles that have comments that match the condition.

Article.find(:joins => :comments ...) 


selection of duplicate records and

Article.find(:include => :comments ...) 


will also extract the Comment data, I just want to get the uniq article data


source to share

2 answers

You can try using"DISTINCT articles.*").joins(:comments).where(...)


or with the syntax you are using

Article.find(:all, :joins => :comments, :select => 'DISTINCT articles.*' ...) 




For example, you are trying to get all articles with current user comments

Article.joins(:comment).where(:comment => {author:}).group_by("comments.article_id")




All Articles