title: Forcing ActiveRecord to count distinct (with Kaminari)
body: <p>I'm using Kaminari to paginate some result from a query in which I'm selecting distinct records. Consider the following controller code:</p>
  
  <pre><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)
  </code></pre>
  
  <p>The best solution that I can think of is to pass <code>:distinct =&gt; true</code> to <code>count</code>, like in <a href="https://github.com/amatsuda/kaminari/pull/77" rel="nofollow noreferrer">this pull request</a>, which was rejected by Kaminari's developer. <a href="https://stackoverflow.com/questions/5092745/rails-3-difference-between-relation-count-and-relation-all-count">This SO question</a> discusses the underlying problem. <a href="https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/models/active_record_relation_methods.rb#L21" rel="nofollow noreferrer">This line of code</a> is the offending call to <code>count</code>.</p>
  
  <p>Are there any workarounds that will provide Kaminari with the correct count that don't involve patching Kaminari? Thanks. </p>
  
  <p>UPDATE:</p>
  
  <ul>
  <li>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.</li>
  </ul>