title: Trying to sort by count of association in rails
body: <p>I have a User model and a PullRequest model - a user has many PullRequests and a PullRequest belongs to User. I have a counter column for keeping track of the total number of PullRequests a user has - but I want to be able to find how many a user has after a specified date. I do this using: </p>
  
  <pre><code>  has_many :pull_requests_as_creator_sprint, -&gt; {where("merged_at &gt;= ?", "2015-10-22")}, class_name:'PullRequest', foreign_key: 'creator_id', dependent: :destroy
  </code></pre>
  
  <p>but how can I use this to sort my data in the pull request controller? </p>
  
  <p>Currently I have <code>@team_members = User.order(sort_column + " " + sort_direction, 'pull_requests_created_count DESC').includes(:pull_requests_as_merger_sprint, :pull_requests_as_creator_sprint)
  </code></p>
  
  <p>which displays by total created count - but I'd like to order by created by since </p>
  
  <pre><code>  has_many :pull_requests_as_creator, class_name:'PullRequest', foreign_key: 'creator_id', dependent: :destroy
    has_many :pull_requests_as_creator_sprint, -&gt; {where("merged_at &gt;= ?", "2015-10-22")}, class_name:'PullRequest', foreign_key: 'creator_id', dependent: :destroy
    has_many :pull_requests_as_merger, class_name:'PullRequest', foreign_key: 'merger_id', dependent: :destroy
    has_many :pull_requests_as_merger_sprint, -&gt; {where("merged_at &gt;= ?", "2015-10-22")}, class_name:'PullRequest', foreign_key: 'merger_id', dependent: :destroy
  </code></pre>
  
  <p>Is my user model - The :pull_requests_as_creator/merger relations are fine, they just use a counter cache. But I want to be able to also sort my data by pull_requests since a date.</p>
  
  <pre><code>  belongs_to :creator, class_name: 'User'
    belongs_to :merger, class_name: 'User'
  </code></pre>
  
  <p>Is my user model.</p>