2011-07-17 95 views
0

我怎麼能從軌道查詢紅寶石這個結果?紅寶石軌道:等效軌道查詢得到這個 - >

select post_id, count(post_id) 
from comments 
group by post_id 
order by count(post_id) desc 
limit 5 
+1

我建議你接受一些答案,然後看看這個指南http://guides.rubyonrails.org/active_record_querying.html – bassneck

+0

我想得到那些帖子的評論最多限制5 – railshero

回答

0

活躍的關係是驚人的。它只是這樣的:

Comment.select("post_id, COUNT(post_id)").group("post_id").order("COUNT(post_id) DESC").limit(5).all 

你應該看看更多,它真的很棒。

+0

謝謝jesse,但我(「post_id,COUNT(post_id)」)。group(「post_id」)。order(「COUNT(post_id)DESC」)。limit(5).all,我怎麼能在一個查詢中做到這一點? – railshero

+0

我不理解你的評論。 – alste

+0

我也不明白 –