在rails3中的连接查询中获取不同的记录

peo*_*eon 3 activerecord ruby-on-rails

一篇文章有​​很多评论,我想获取所有评论符合条件的文章.

Article.find(:joins => :comments ...) 
Run Code Online (Sandbox Code Playgroud)

获取重复的记录和

Article.find(:include => :comments ...) 
Run Code Online (Sandbox Code Playgroud)

还会获取Comment数据,我只想获取uniq文章数据

Pav*_*l S 9

你可以尝试使用

Article.select("DISTINCT articles.*").joins(:comments).where(...)
Run Code Online (Sandbox Code Playgroud)

或者您正在使用的语法

Article.find(:all, :joins => :comments, :select => 'DISTINCT articles.*' ...) 
Run Code Online (Sandbox Code Playgroud)