5
我尝试找到顶部n
与文章相关的类别数量,两者之间建立了一种哈比姆关系。这是我想要执行的SQL,但我不确定如何使用ActiveRecord执行此操作,除了使用find_by_sql
方法。有没有与ActiveRecord的方法,这样做的任何方式:高级计数和加入Rails
SELECT
"categories".id,
"categories".name,
count("articles".id) as counter
FROM "categories"
JOIN "articles_categories"
ON "articles_categories".category_id = "categories".id
JOIN "articles"
ON "articles".id = "articles_categories".article_id
GROUP BY "categories".id
ORDER BY counter DESC
LIMIT 5;
优秀的感谢,以为我试过了,但很明显,我有什么错它 – trobrock 2010-06-02 01:23:00
任何方式后访问计数值? 'category = Category.find(...)。first; category.counter'? – 2016-06-09 14:01:08