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

+3


source to share


2 answers


You can try using

Article.select("DISTINCT articles.*").joins(:comments).where(...)

      



or with the syntax you are using

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

      

+9


source


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



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

      

+1


source







All Articles