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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:09:19+00:00 2026-05-16T23:09:19+00:00

I am putting together a small app that has a leaderboard concept in it.

  • 0

I am putting together a small app that has a leaderboard concept in it. Basically, the model is just a player_name and a current_score.

what I want to do is get the ranking of a specific player, and given that new scores are coming in all the time, it needs to be dynamic. Obviously I could just do a normal find with an order by clause and then I would have to loop through each record. Not very efficient when I could have 100,000’s of rows.

Any suggestions on what approach I should be taking?

Here is the migration for the table:

class CreateScores < ActiveRecord::Migration
  def self.up
    create_table :scores do |t|
      t.string  :player_name
      t.integer :current_score

      t.timestamps
    end
  end

  def self.down
    drop_table :scores
  end
end

EDIT
As an example, so I have the following:

Score.select('player_name, current_score').limit(20)
=> [#<Score player_name: "Keith Hughes", current_score: 9>, #<Score player_name: "Diane Chapman", current_score: 8>, #<Score player_name: "Helen Dixon", current_score: 4>, #<Score player_name: "Donald Lynch", current_score: 9>, #<Score player_name: "Shawn Snyder", current_score: 2>, #<Score player_name: "Nancy Palmer", current_score: 9>, #<Score player_name: "Janet Arnold", current_score: 1>, #<Score player_name: "Sharon Torres", current_score: 9>, #<Score player_name: "Keith Ortiz", current_score: 5>, #<Score player_name: "Judith Day", current_score: 3>, #<Score player_name: "Gregory Watson", current_score: 7>, #<Score player_name: "Jeremy Welch", current_score: 3>, #<Score player_name: "Sharon Oliver", current_score: 7>, #<Score player_name: "Donald Lewis", current_score: 7>, #<Score player_name: "Timothy Frazier", current_score: 7>, #<Score player_name: "John Richards", current_score: 1>, #<Score player_name: "Carolyn White", current_score: 4>, #<Score player_name: "Ronald Smith", current_score: 2>, #<Score player_name: "Emily Freeman", current_score: 9>, #<Score player_name: "Gregory Wright", current_score: 2>]

How can I work out the ranking of “Donald Lewis” ?

  • 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-16T23:09:19+00:00Added an answer on May 16, 2026 at 11:09 pm

    You could count the number of records that have a higher current_score. You will still have to do it per record (or do a sub-query in your sql).

    class Score < ActiveRecord::Base
    
      def ranking
        Score.count(:conditions => ['current_score > ?', self.current_score])
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm putting together a small web app that writes to a database (Perl CGI
I am putting together a fairly simple web app that uses user inputted data
I am putting together a small app to perform automated checkouts on a Magento
I'm putting together a simple drop down that contains nested ul's. I want it
I am putting together a small debugger for python so I can basically attach
I'm putting together an app that will show a news item's location in Google
I'm parsing various site feeds, and putting together a small library to help me
I'm putting together a simple search engine for a small site. Users will be
I putting together a page that will display a set of stored values. I
I'm putting together a demo web-app. I've created a certificate signed by my own

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.