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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:00:46+00:00 2026-06-15T09:00:46+00:00

I have a Department model with two associations to User such that class Department

  • 0

I have a Department model with two associations to User such that

class Department < ActiveRecord::Base
   has_many :members, class_name:"User", foreign_key:"department_id"
   belongs_to :director, class_name:"User", foreign_key:"director_id
end

To display this model in a listing, sorted by number of members I can do this

@departments = Department.select("departments.*, COUNT(users.id) AS members").joins(:members).group("departments.id")
@departments = @departments.order("members ASC")

If I wanted to sort by a field (last_name) in the director’s associated model, I could do this:

@departments = Department.includes(:director)
@departments = @departments.order("users.last_name")

However, I can’t combine these two queries into one that allows correct sorting of both. If I do

Department.select("departments.*, COUNT(users.id) AS members").joins(:members, :director).group("departments.id, users.last_name")

then I get a vast ammount of duplicate results, and I also exclude resuls where director_id is nil. How can I structure the query so that I can order by both COUNT(users.id) and essentially director.last_name ?

Edit: This is the .to_sql for that bad combined query:

SELECT departments.*, COUNT(users.id) AS members FROM "departments" 
INNER JOIN "users" ON "users"."department_id" = "departments"."id" 
INNER JOIN "users" "directors_departments"    
ON "directors_departments"."id" = "departments"."director_id" 
GROUP BY departments.id, users.last_name 
ORDER BY users.last_name DESC
  • 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-15T09:00:47+00:00Added an answer on June 15, 2026 at 9:00 am

    So there are 2 problems here:

    1. You are using .joins on a belongs_to association that may not exist.
    2. You are joining the same user table twice (for 2 different associations), causing rails to generate a table alias so that the SQL will be valid. But you aren’t grouping on this alias.

    Solution:

    You’ll need a LEFT JOIN which unfortunately isn’t as pretty as a standard join. You’ll also need to modify your .group clause:

    Department.
      select("departments.*, COUNT(users.id) AS members").
      joins(:members, "LEFT JOIN users directors ON departments.director_id = directors.id").
      group("departments.id, directors.last_name").
      order("members, directors.last_name")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two model like so class User < ActiveRecord::Base attr_accessible :email, :password, :password_confirmation,
I have a very simple issue: User model: class User < ActiveRecord::Base devise :database_authenticatable,
In my model I have an abstract User class, and multiple subclasses such as
I got a problem, i've got two models class Department < ActiveRecord::Base belongs_to :school
I have two dropdown in a form for selecting department and designation. $model =
I have a User model with attributes department and points. I have grouped the
I have two models with the following associations: A user has many projects. A
I have a module with two models that extend Zend_Db_Table_Abstract: class Departments_Model_Subpages extends Zend_Db_Table_Abstract
I have a model defined as public class Department { [ScaffoldColumn(false)] public object Id
I am using asp.net MVC2. I have a model defined as public class Department

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.