I have 2 models, User and Purchase.
A User has_many :purchases and a Purchase belongs_to :user
Purchases fields are:
id, product_id, user_id, amount, created_at
What I am trying to achieve is a method call such as:
User.top_five which would return the 5 Users with the highest purchase value, i.e. the sum of the purchases.amount field for each user.
I also want to be able to do something like User.top_five(:start_date=>'01/01/2010',:end_date=>'31/12/2010') , i.e. select a time period to calculate the top five users.
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.
thanks
I suggest something like