开发者

Will_paginate gives 'unusual' result on count when using group clause

开发者 https://www.devze.com 2023-04-11 01:44 出处:网络
Using Rails3 and will_paginate 3.0.2 and seeing an unusual issue: Rating.paginate(:page => 1).count => 3

Using Rails3 and will_paginate 3.0.2 and seeing an unusual issue:

Rating.paginate(:page => 1).count

=> 3

BUT if I add the group clause:

Rating.paginate(:page => 1, :group => "drill_id").count

=> {3=>2, 4=>1}

The closest google result I found was this: https://github.com/mislav/will_paginate/issues/167

But this doesnt seem to b开发者_开发技巧e the exact same issue.Any ideas?


You are getting a grouped result from #count. It's basically a count of Rating per drill_id. The keys of the result hash are drill_ids with their corresponding values being the count of Ratings for this drill_id. See AR::Calculations#count for details.

You want to know how many drill_ids there are between all your Ratings instead? Choose one:

  • Fix the query

    Rating.select('DISTINCT drill_id').count

  • Count the keys

    Rating.paginate(page: 1, group: 'drill_id').keys.count

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号