所以我3.2的代码看起来像这样的Rails 4.1的升级,具有组麻烦
AssocGenre.includes(:genre).where(attachable_type: Project).count(group: 'genres.name').sort_by{|k,v| -v}.each do
但现在它给我这个错误
undefined method `sort_by' for 193:Fixnum
,这是什么在轨4.1,正确的语法?
所以我3.2的代码看起来像这样的Rails 4.1的升级,具有组麻烦
AssocGenre.includes(:genre).where(attachable_type: Project).count(group: 'genres.name').sort_by{|k,v| -v}.each do
但现在它给我这个错误
undefined method `sort_by' for 193:Fixnum
,这是什么在轨4.1,正确的语法?
你以前可以调用count
但不是当了指定GROUP BY子句。现在您必须使用单独的group
呼叫来指定GROUP BY。从fine manual:
计数(列=零,选项= {})
对记录进行计数。
[...]
如果count
使用与group
,它返回一个Hash,它的键代表集合列,和值是各自的金额:Person.group(:city).count # => { 'Rome' => 5, 'Paris' => 3 }
你可能想包括简单的INNER JOIN在SQL中而不是所有额外的东西includes
增加所以joins
应该更好地工作。
所以,你要这样说写信:
AssocGenre.joins(:genre)
.where(attachable_type: Project)
.group('genres.name')
.count
.sort_by ...
工作很好,但我不得不改变包括加入。 – TuxedoTomson 2014-09-24 16:49:28
对不起,我根本没有注意到'includes'。 – 2014-09-24 17:08:15
只是有一个尝试:
AssocGenre.includes(:genre).where(attachable_type: Project).count(group:'genres.name').**to_a**.sort_by{|k,v| -v}.each.....
这不起作用,'count'不需要在Rails4中提供GROUP BY子句的':group'选项。 – 2014-09-21 01:54:52
什么是你想排序/算? – Anthony 2014-09-19 01:16:57
偶然有193个匹配您的查询吗? – 2014-09-19 04:07:37