title: Ruby on Rails, find top 5 Users ordered by purchases
body: <p>I have 2 models, User and Purchase.
  A <code>User has_many :purchases</code> and a <code>Purchase belongs_to :user</code></p>
  
  <p>Purchases fields are:</p>
  
  <pre><code>id, product_id, user_id, amount, created_at
  </code></pre>
  
  <p>What I am trying to achieve is a method call such as:
  <code>User.top_five</code> which would return the 5 Users with the highest purchase value, i.e. the sum of the purchases.amount field for each user.</p>
  
  <p>I also want to be able to do something like <code>User.top_five(:start_date=&gt;'01/01/2010',:end_date=&gt;'31/12/2010')</code> , i.e. select a time period to calculate the top five users.</p>
  
  <p>So I've been trying to get the right combination of joins, sums, order_bys etc, but I'm just not getting it. So hopefully someone can point me in the right direction!
  Hopefully I've given enough info here, this is my first question.</p>
  
  <p>thanks</p>