Ruby on rails 可能会覆盖count的工作方式,或者找到更好的方法来实现这一点

Ruby on rails 可能会覆盖count的工作方式,或者找到更好的方法来实现这一点,ruby-on-rails,arel,Ruby On Rails,Arel,在我的艺术家模型中,我有这样一个范围,它给了我艺术家,按照他们在某个时间段内的受欢迎程度排序popularity scope :by_popularity, lambda { |*args| options = (default_popularity_options).merge(args[0] || {}) select("SUM(popularity) AS popularity, artists.*"). from("popularity_caches FORCE INDEX (

在我的艺术家模型中,我有这样一个范围,它给了我艺术家,按照他们在某个时间段内的受欢迎程度排序<每天计算
popularity\u caches
表中的code>popularity

scope :by_popularity, lambda { |*args|
  options = (default_popularity_options).merge(args[0] || {})

  select("SUM(popularity) AS popularity, artists.*").
from("popularity_caches FORCE INDEX (popularity_cache_group), artists FORCE INDEX (index_artists_on_id_and_genre_id)").
where("popularity_caches.target_type = 'Artist'").
where("popularity_caches.target_id = artists.id").
where("popularity_caches.time_frame = ?", options[:time_frame]).
where("popularity_caches.started_on > ?", options[:started_on]).
where("popularity_caches.started_on < ?", options[:ended_on]).
group("artists.id").
order("popularity DESC")
}
这就是我可能希望得到的SQL:

SELECT COUNT(DISTINCT(artists.id)) AS count_all
FROM popularity_caches FORCE INDEX (popularity_cache_group), artists FORCE INDEX (index_artists_on_id_and_genre_id)
WHERE (popularity_caches.target_type = 'Artist')
  AND (popularity_caches.target_id = artists.id)
  AND (popularity_caches.time_frame = 'week')
  AND (popularity_caches.started_on > '2011-02-28 16:00:00')
  AND (popularity_caches.started_on < '2011-10-05')
ORDER BY popularity DESC
我闻到了,因为它很脆

在阿雷尔有办法做到这一点吗?可能会覆盖它计算事物的方式(distinct artists.id)并删除
group by
,这样它就不会为计数返回哈希

谢谢

以惊人的方式解决:

SELECT COUNT(DISTINCT(artists.id)) AS count_all
FROM popularity_caches FORCE INDEX (popularity_cache_group), artists FORCE INDEX (index_artists_on_id_and_genre_id)
WHERE (popularity_caches.target_type = 'Artist')
  AND (popularity_caches.target_id = artists.id)
  AND (popularity_caches.time_frame = 'week')
  AND (popularity_caches.started_on > '2011-02-28 16:00:00')
  AND (popularity_caches.started_on < '2011-10-05')
ORDER BY popularity DESC
@artists = Artists.by_popularity(some args).paginate(
  :total_entries => Artist.count_by_popularity(pass in the same args here as in Artist.by_popularity),
  :per_page => 5,
  page => ...
)
PopularityCach.select(
  Arel::Nodes::Group.new(Artist.arel_table[:id]).count.as('count_all')
).where(
  PopularityCach.arel_table[:target_type].eq('Artist').and(
    PopularityCach.arel_table[:target_id].eq(Artist.arel_table[:id]).and(
      PopularityCach.arel_table[:time_frame].eq('week').and(
        PopularityCach.arel_table[:started_on].gt('2011-02-28 16:00:00').and(
          PopularityCach.arel_table[:started_on].lt('2011-10-05')
        )
      )
    )
  )
).order(:popularity).reverse_order