这个查询
Message.where("message_type = ?", "incoming").group("sender_number").count
会给我一个哈希值。
OrderedHash {"1234"=>21, "2345"=>11, "3456"=>63, "4568"=>100}
现在,我想按每个组的数量进行排序。我如何在查询中做到这一点。
最佳答案
最简单的方法是在原始查询中添加一个order子句。如果您给count方法指定一个特定的字段,它将生成一个名称为count_ {column}的输出列,该列可在通过添加订单调用生成的sql中使用:
Message.where('message_type = ?','incoming')
.group('sender_number')
.order('count_id asc').count('id')