Activerecord查询帮助我

时间:2015-04-15 09:30:24

标签: ruby-on-rails

如何在activerecord中进行查询:

SELECT col1, COUNT(col1) AS count FROM tab1 
WHERE col2='sometext' 
GROUP BY col1 
ORDER BY count DESC 
LIMIT 10

预期产出:

col1    count
--------------
name1   247
name5   182
name4   96

1 个答案:

答案 0 :(得分:0)

这是:

Model
  .select('col1, COUNT(col1) AS count')
  .where(col2: 'sometext')
  .group(:col1)
  .order('count DESC')
  .limit(10)