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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:52:45+00:00 2026-05-24T20:52:45+00:00

If User s have points, how can I get the user rank, assuming standard

  • 0

If Users have points, how can I get the user rank, assuming standard positioning:

require 'active_record'

class User < ActiveRecord::Base
  def rank
    # ???
  end
end

User.all
# => [<User id:1 points:100>, <User id:2 points:400>, <User id:3 points:100>, <User id:4 points:250>]

User.find_by_id(2).rank
# => 1
User.find_by_id(3).rank
# => 3
User.find_by_id(1).rank
# => 3

I get the distinct impression this will be database intensive. I’m not too worried about that, but obviously solutions which require less processing power are winners!

If giving two users with the same number of points the same rank is too complex, I’m prepared to accept that user 1 above may be rank 3, and user 3 may be rank 4.

EDIT:

I’m actually storin gmy scores in a seperate class – as each event warrants a certain amount of points.

class Event < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :events

  def points
    events.sum(:amount)
  end

  def rank
    # This seems a bit ham-handed
    Event.count_by_sql(['select count(*) from (select sum(amount) as points from events group by user_id) where points > ?',points]) + 1
  end
end

Any ideas as to how to make this more succinct?

Thanks in advance

  • 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-24T20:52:46+00:00Added an answer on May 24, 2026 at 8:52 pm

    You can count the number of users having less points, and add one.

    def rank
      User.where("points > ?", points).count + 1
    end
    

    This would return 3 for users 1 and 3.

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

Sidebar

Related Questions

I have table Users which contains columns: id, name, points, extra_points. How can I
I have a User table which has a PrivilegeId foreign key points to a
I have the following code which lets the user plot two points on a
Please, I'm trying to get some total value of user earned points in few
I have the following model: model rank(Models.model): username = models.CharField(max_length=200, default=0) points = models.IntegerField(default=0)
I have a UIImageView in a UITableView Header. How can I get the exact
When a user have changed something in a form, then clicks on any link
I need to know which views does a user have which privileges on. I'm
I have user control named DateTimeUC which has two textboxes on its markup: <asp:TextBox
I have a user control let say UC1 . This user control have viewmodel

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.