0

我有一行代码看起来像这样

    @words =  Word.connection.select_all("select text, count(*) as occurs from words join works on words.work_id = works.id where works.grouping == 'group1' group by text order by occurs desc limit 10")

我想用一种更方便/更活跃的记录方式来写它,但语法对我来说还是很新的。

每部作品都有很多词,每个词都属于一部作品。

4

1 回答 1

0

也许这段代码对你有用:

Word.includes(:works).where("works.grouping == 'group1'").limit(10).count(:group => :text)

includes方法创建与您的工作表的连接。该where方法是针对条件的,它limit是不言自明的,并且也是countgroup一列组成的。

于 2012-09-12T14:09:02.410 回答