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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:00:53+00:00 2026-05-24T16:00:53+00:00

In my application Users register for Events , which belong to a Stream .

  • 0

In my application Users register for Events, which belong to a Stream. The registrations are managed in the Registration model, which have a boolean field called ‘attended‘.

I’m trying to generate a leaderboard and need to know: the total number of registrations for each user, as well as a count for user registrations in each individual event stream.

I’m trying this (in User.rb):

# returns an array of users and their attendence count
def self.attendance_counts
    User.all( 
      :select => "users.*, sum(attended) as attendance_count", 
      :joins => 'left join `registrations` ON registrations.user_id = users.id', 
      :group => 'registrations.user_id', 
      :order => 'attendance_count DESC'
    )
end

The generated SQL works for just returning the total attended count for each user when I run it in the database, but all that gets returned is the User record in Rails.

I’m about to give up and hardcode a counter_cache for each stream (they are fairly fixed) into the User table, which gets manually updated whenever the attended attribute changes on a Registration model save.

Still, I’m really curious as to how to perform a query like this. It must come up all the time when calculating statistics and reports on records with relationships.

Your time and consideration is much appreciated. 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-24T16:00:54+00:00Added an answer on May 24, 2026 at 4:00 pm

    Firstly as a couple of points on style and rails functions to help you with building DB queries.

    1) You’re better writing this as a scope rather than a method i.e.

    scope attendance_counts, select("users.*, sum(attended) as attendance_count").joins(:registrations).group('registrations.user_id').order('attendance_count DESC')
    

    2) It’s better not to call all/find/first on the query you’ve built up until you actually need it (i.e. in the controller or view). That way if you decide to implement action / fragment caching later on the DB query won’t get called if the cached action / fragment is served to the user.

    3) Rails has a series of functions to help with aggregating db data. for example if you only wanted a user’s id and the sum of attended you could use something like the following code:

    Registrations.group(:user_id).sum(:attended)
    

    Other functions include count, avg, minimum, maximum

    Finally in answer to your question, rails will create an attribute for you to access the value of any custom fields you have in the select part of your query. e.g.

    @users = User.attendance_counts
    @users[0].attendance_count # The attendance count for the first user returned by the query
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a freelance web application that lets users register for events. In my
We are developing and web application which allows users to register for certain events.
I want to build a web application where users can register and have a
In my web application i have registration form, when user register i want to
In the application I'm building, I want to have Events. People can then register
I have a really simple Rails application that allows users to register their attendance
I have an idea to build an application where users can register and keep
I want to create a web application that allows users to sign up, register
In my application users cannot truly delete records. Rather, the record's Deleted field gets
As part of a web application users can upload files of data, which generates

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.