0

我在我的模型中使用范围,比如

     scope :by_average_rating_than_no_of_rating, :order => 'average_rating DESC,number_of_ratings DESC,editors_pick DESC'

但我想达到如下结果

     scope :by_average_rating_than_no_of_rating, :order => 'average_rating DESC,count(number_of_ratings) DESC,editors_pick DESC'

我如何按顺序对模型属性应用计数?

有什么帮助吗?

谢谢

4

1 回答 1

1

您缺少用于计算评分数量的 select 语句。

这是我如何获得的示例,

class Song
  has_many :listens
  scope :top5,
    select("songs.id, OTHER_ATTRS_YOU_NEED, count(listens.id) AS listens_count").
    joins(:listens).
    order("listens_count DESC").
    limit(5)

Song.top5 # top 5 most listened songs
于 2013-04-03T09:39:01.030 回答