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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:51:06+00:00 2026-06-11T13:51:06+00:00

Let’s say I have two classes : User and Task. A task belongs to

  • 0

Let’s say I have two classes : User and Task. A task belongs to a user and has a priority (which is an integer between 1 and 3, or nil).

class User < ActiveRecord::Base
    has_many :tasks

class Task < ActiveRecord::Base
    belongs_to :user
    attr_accessible :priority # Integer between 1 and 3, or nil

I want to present a TOP 10 of users with the highest number of tasks and the highest priority (sort first by high priority then medium then low then unevaluated). I have thought of a different way but I can’t find a clean way for handling this.

I first thought of using a complex SQL request in the controller’s method for limiting performance:

User.joins(:tasks).count(:all, :group => ["tasks.user_id", "priority"])

However the result is not easy to be sorted and I finally have to use two loops for sorting and another one to render the Users. Furthermore, I think that this kind of method would be more logically in the model.

What do you think? What is the cleanest way to handle this?

  • 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-11T13:51:07+00:00Added an answer on June 11, 2026 at 1:51 pm

    If I understand correctly you want to sort the users who has the highest number of tasks with the highest priority right? So in a scenario:

    • User A has 3 tasks, 2 with priority 1 and 1 with 3
    • User B has 2 tasks, 2 with Priority 3
    • User C has 1 task with priority 2

    And you’d expect the output to be: B, A, C?

    I would just sum the task priorities and sort by that then:

    User.joins(:tasks).group("users.id").order("SUM(tasks.priority) DESC").first(10)
    

    The SQL may be a bit off as I am writing this off the top of my head, but you hopefully get the idea. The trick here is not so much the SQL but rather the weighting function you use to sort.

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

Sidebar

Related Questions

Let's say I have a method in java, which looks up a user in
Let's say I have two objects, Master and Slave . Slave has a method
Let's say I have 2 windows in my application, and two classes responsible for
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say I have an facebook application running using the JS SDK. First user
Let's say I have the following classes : public class MyProductCode { private String
Let's say you have two different categories of users with different profiles: one for
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following two lists of tuples myList = [(1, 7),

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.