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 8113295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:46:51+00:00 2026-06-06T02:46:51+00:00

I’m trying to do some fairly complicated record sorting that I was having a

  • 0

I’m trying to do some fairly complicated record sorting that I was having a bit of trouble with. I have three models:

    class User < ActiveRecord::Base

    has_many :registers
    has_many :results, :through => :registers

    #Find all the Users that exist as registrants for a tournament
    scope :with_tournament_entrees, :include => :registers, :conditions => "registers.id IS NOT NULL"

end

Register

class Register < ActiveRecord::Base
  belongs_to :user
  has_many :results
end

Result

class Result < ActiveRecord::Base
  belongs_to :register 
end

Now on a Tournament result page I list all users by their total wins (wins is calculated through the results table). First thing first I find all users who have entered a tournament with the query:

User.with_tournament_entrees

With this I can simply loop through the returned users and query each individual record with the following to retrieve each users “Total Wins“:

user.results.where("win = true").count()

However I would also like to take this a step further and order all of the users by their “Total Wins“, and this is the best I could come up with:

User.with_tournament_entrees.select('SELECT *, 
                                    (SELECT count(*)  
                                     FROM results 
                                     INNER JOIN "registers" 
                                     ON "results"."register_id" = "registers"."id" 
                                     WHERE "registers"."user_id" = "users.id" 
                                     AND (win = true)
                                    ) AS total_wins 
                                     FROM users ORDER BY total_wins DESC')

I think it’s close, but it doesn’t actually order by the total_wins in descending order as I instruct it to. I’m using a PostgreSQL database.

Edit:

There’s actually three selects taking place, the first occurs on User.with_tournament_entries which just performs a quick filter on the User table. If I ignore that and try

SELECT *, (SELECT count(*) FROM results INNER JOIN "registers" ON "results"."register_id" = "registers"."id" WHERE "registers"."user_id" = "users.id" AND (win = true)) AS total_wins FROM users ORDER BY total_wins DESC; 

it fails in both PSQL and the ERB console. I get the error message:

PGError: ERROR: column "users.id" does not exist

I think this happens because the inner-select occurs before the outer-select so it doesn’t have access to the user id before hand. Not sure how to give it access to all user ids before than inner select occurs but this isn’t an issue when I do User.with_tournament_entires followed by the query.

  • 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-06T02:46:54+00:00Added an answer on June 6, 2026 at 2:46 am

    In your SQL, "users.id" is quoted wrong — it’s telling Postgres to look for a column named, literally, “users.id”.

    It should be "users"."id", or, just users.id (you only need to quote it if you have a table/column name that conflicts with a postgres keyword, or have punctuation or something else unusual).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

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.