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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:11:21+00:00 2026-06-12T02:11:21+00:00

I have a Rails application with the following models: User Bet User has many_bets

  • 0

I have a Rails application with the following models:

  • User
  • Bet

User has many_bets and Bets belongs_to User. Every Bet has a Profitloss value, which states how much the User has won/lost on that Bet.

So to calculate how much a specific User has won overall I cycle through his bets in the following way:

 User.bets.sum(:profitloss)

I would like to show the User his ranking compared to all the other Users, which could look something like this:

“Your overall ranking: 37th place”

To do so I need to sum up the overall winnings per User, and find out in which position the current user is.

How do I do that and how to do it, so it don’t overload the server 🙂

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-06-12T02:11:22+00:00Added an answer on June 12, 2026 at 2:11 am

    You can try something similar to

    User.join(:bets).
        select("users.id, sum(bets.profitloss) as accumulated").
        group("users.id").
        order("accumulated DESC")
    

    and then search in the resulting list of “users” (not real users, they have only two meaningful attributes, their ID and a accumulated attribute with the sum), for the one corresponding to the current one.

    In any case to get a single user’s position, you have to calculate all users’ accumulated, but at least this is only one query. Even better, you can store in the user model the accumulated value, and query just it for ranking.

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

Sidebar

Related Questions

I have a rails application with the following Models: User (id) Version (id, post_id,
In my rails application I have following models Transaction belongs_to :account belongs_to :agent belongs_to
I have the following models in my Rails application: class Shift < ActiveRecord::Base has_many
My rails application has two models, gallery and photo. Galleries have many photos and
I have an application in Rails 3.2.1 with a model which has a column
I have the following code in my Rails application's routes file: MyApp::Application.routes.draw do constraints
I have the following code in one of my Rails 3 application views: <ul>
In my Rails 3.2.3 application I have the following code in the application.html.erb to
In my rails application my models have a to_html method. This method is called
I have two models/tables in my Rails application: discussions and comments. Each discussion has_many

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.