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

  • SEARCH
  • Home
  • 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 8772155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:58:18+00:00 2026-06-13T17:58:18+00:00

I have the following mysql query working well. Quick backstory, this lists our customers

  • 0

I have the following mysql query working well. Quick backstory, this lists our customers by total money spent.

SELECT  SUM(price) AS money_spent, co.customer_id, cu.first_name, cu.last_name, cu.email_primary
FROM customer_order AS co 
JOIN customer AS cu ON (cu.customer_id = co.customer_id) 
GROUP BY co.customer_id
ORDER BY money_spent DESC

I attempted moving this to the customer model, as a scope, and subclass yet I failed both ways. I’m somewhat new to rails (formerly client-side) but I think I’m close. Hoping y’all could point me in the right direction. Thanks in advance!!!

def self.high_rollers  
  options = {
      :select   => "SUM(price) AS money_spent, co.customer_id, cu.first_name, cu.last_name, cu.email_primary",
      :from     => "customer_order AS co",
      :joins    => "customer AS cu ON (cu.customer_id = co.customer_id)",
      :group_by => "co.customer_id",
      :order    => "money_spent DESC"
            }   

Putting the following code in my index action for this controller works, but definitely doesn’t feel like the right approach!

@high_bidders = Customer.find_by_sql(<<-SQL
       SELECT  SUM(price) AS money_spent, co.customer_id, cu.first_name, cu.last_name, cu.email_primary
       FROM customer_order AS co 
       JOIN customer AS cu ON (cu.customer_id = co.customer_id) 
       GROUP BY co.customer_id
       ORDER BY money_spent DESC

SQL
)

  • 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-06-13T17:58:19+00:00Added an answer on June 13, 2026 at 5:58 pm

    I assume your model names are Customer and CustomerOrder. Your table names aren’t conventional, I think you already explicitly set table names in models with self.table_name.

    class CustomerOrder < ActiveRecord::Base
      # ...
      belongs_to :customer
    
      scope :high_rollers, select("*, SUM(price) AS money_spent").
                           joins(:customer).
                           group(:customer_id).
                           order("money_spent DESC")
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following, working MySQL query: SELECT a.id id, a.price price, a.stock stock,
I have the following mysql query. Could you please tell me how to write
I have the following mysql query, which is producing a list of entries by
I have the following MySQL-query: SELECT s.student_socialnr, s.student_lastname, s.student_firstname, cs.city_name, scp.cpl_startdate, scp.cpl_enddate, scp.cpl_invoice, cu.customer_name,
I have the following mysql query which I am running with php like so.
I have the following MySQL query statement: SELECT * FROM table1 INNER JOIN table2
I have the following mysql query: $manufacturers_query = select m.manufacturers_id, m.manufacturers_name, m.manufacturers_image, mi.manufacturers_url from
I have an application that executes the following MySQL query: SELECT 402 AS user_id,
I have following query and data saved in a MySQL database with a timestamp
I have the following query that executes perfectly in MySQL, but it is giving

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.