I'm using Kaminari to paginate some result from a query in which I'm selecting distinct records. Consider the following controller code:
@things = Thing.joins... # create a complex query that produces duplicate results
# I want to select distinct results: this produces the correct results
@things = @things.select("DISTINCT things.*")
# when Kaminari calls count, it will run "SELECT COUNT(*)", instead of
# "SELECT COUNT(DISTINCT things.*)" we will get the wrong count and extra pages
@things = @things.page(params[:page]).per(10)
The best solution that I can think of is to pass :distinct => true to count, like in this pull request, which was rejected by Kaminari's developer. This SO question discusses the underlying problem. This line of code is the offending call to count.
Are there any workarounds that will provide Kaminari with the correct count that don't involve patching Kaminari? Thanks.
UPDATE:
- Using a scope called "count" is a great suggestion but doesn't work when called on an ActiveRecord::Relation. It works when called on my model class, but that doesn't help.