title: Ordering objects by a sum of has_many association
body: <p>I have two objects:</p>
  
  <pre><code>class Person &lt; ActiveRecord::Base
     has_many :tests
  end
  
  class Test &lt;ActiveRecord::Base
     belongs_to :person
  end
  </code></pre>
  
  <p>I need to sort all my person objects by the sum of scores on the test. One other trick is each person may not have a test.</p>
  
  <p>I've gotten as far as this with my tests but I can't get them to order based on the sum of the scores for that person because I can't figure out how to reference the value being calculated.</p>
  
  <pre><code>Score.sum(:score, :group =&gt; :person_id)
  </code></pre>
  
  <p>Once I have this how do I get this back to a list of players to sort?</p>