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

  • Home
  • SEARCH
  • 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 6991567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:31:12+00:00 2026-05-27T19:31:12+00:00

Here is the relevant data structure and associations: User (has_many completions) – id Quest

  • 0

Here is the relevant data structure and associations:

User (has_many completions)
- id

Quest (has_many completions)
- id
- points [amount that earned if completed]

Completions (belongs_to users and quests)
- user_id
- quest_id

Basically, Completions is a join table populated when a user completes a quest. The part I am struggling with is how to calculate the total points earned, because it has to reach back to the Quest table to see how much each was worth.

Example:

 Quests
  id 1
  points 25
  --
  id 2
  points 10
  --
  id 3
  points 50

 Completions
  user_id 1
  quest_id 1
  --
  user_id 1
  quest_id 2
  --
  user_id 2
  quest_id 3

In this example, user_id 1 has earned 35 total points (25+10), and user_id 2 has earned 50 total points.

So two questions:

1) How would I write a query in Rails to allow me to list all users and their total points?

2) Is this the best way to handle this type of functionality?

Thank you!

  • 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-27T19:31:13+00:00Added an answer on May 27, 2026 at 7:31 pm

    Pretty simple to do with a quick reduce and a query.

    class User < ActiveRecord::Base
      has_many :completions
      has_many :quests, :through => :completions
    
      attr_accessor :total_points
    
      # calculates points for individual user
      def points
        @total_points ||= quests.reduce(0) { |sum, quest| sum + quest.points }
        # note this doesn't reload, so if you add a completion without reloading the
        # user object this will be stale
      end
    
      # when getting a list of users, make sure you use an efficient query to
      # fetch all of the associated quests
      # 
      # this is the same as using `scope :with_quests, include(:quests)`
      def self.with_quests
        include(:quests)
      end
    end
    
    # outside of your model:
    @users = User.with_quests.all
    
    # and in a view (e.g. _user.html.erb):
    Points: <%= user.points %>
    

    Cheers!

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

Sidebar

Related Questions

I have a strange problem with mod_rewrite, the rules that are relevant here are:
Here are the relevant models: User Product Order A User can sell or buy
So firstly here is the relevant table structure: TUBE_VIDEOS ------ id video_name TAGS ----
I do not seem to understand how to catch constructor exception. Here is relevant
Here's the relevant code snippet. public static Territory[] assignTerri (Territory[] board, String[] colors) {
Here's the relevant code: public static void printBoarders (Territory x) { int t =
Here's the relevant bit of the source code: class Dice { String name ;
Here's the relevant part of the Dice class: import java.util.*; class Dice { String
I'm having some trouble with the jQuery hover method. Here's the relevant JavaScript code:
I searched through here as best I could and though I found some relevant

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.