Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4257554
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:31:40+00:00 2026-05-21T05:31:40+00:00

I have 2 models, User and Purchase. A User has_many :purchases and a Purchase

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:31:41+00:00Added an answer on May 21, 2026 at 5:31 am

    I suggest something like

    def self.top_five(start_date, end_date)
      User.all(:select => "users.*, SUM(purchased.amount) as purchased_sum",
               :joins => "LEFT JOIN purchases AS purchased ON purchased.user_id = users.id",
               :group => "users.id",
               :conditions => ["purchased.created_at BETWEEN ? AND ?", start_date, end_date],
               :order => "purchased_sum DESC",
               :limit => 5)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 models: User: has_many :comments Video: has_many :comments Comment: belongs_to :video, :user
I have two models: User and HairColor. A user has only one hair color,
say you have 3 models : user, hair_color, and eye_color user hasOne hair_color user
I have 2 models. User: @Entity public class User implements Serializable { Long id;
In my app, I have three models: Markets, Deals, and Users. Relationships are pretty
I am trying to have a choicefield that only displays the users with the
I have models as follows: class Book(models.Model): title = models.CharField(max_length=400) def transactions_all_time(self): latest_transactions =
I'm trying to create a transaction history for each transaction on a Django based
I have a small php app that I want to build a layer of
I'm building the data model for the website of a student club at my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.