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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:03:07+00:00 2026-06-18T06:03:07+00:00

Im having trouble figuring out how to write a multi-layer sort using a scope

  • 0

Im having trouble figuring out how to write a multi-layer sort using a scope method in my model, which can sort through the model’s attributes, as well as its related child models’ attributes?

To put more concretely, I have the following models, each a related child of the previous one (I excluded other model methods and declarations for brevity):

class Course < ActiveRecord::Base 
  has_many :questions
  # would like to write multi-layer sort here 
end 

class Question < ActiveRecord::Base 
  belongs_to :course, :counter_cache => true
  has_many: :answers

end 

class Answer < ActiveRecord::Base 
  belongs_to :question, :counter_cache => true
end

I would like to sort courses first by questions_count (through my counter_cache), then by answer_count, and lastly by created_at, and was wondering how I could string everything together into a single scope method to put in my Course model.

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-18T06:03:09+00:00Added an answer on June 18, 2026 at 6:03 am

    As seen here (creating a scope with a joined model) : problem: activerecord (rails3), chaining scopes with includes

    And here (sorting with multiple columns) : Ruby on Rails: how do I sort with two columns using ActiveRecord?

    And finally here (sorting by associated model) : Rails 3. sort by associated model

    You may achieve this like so :

    scope :complex_sorting, lambda {
        joins(:questions)
        .order('questions_count DESC, question.answers_count DESC, created_at DESC')
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble figuring out how to write to a file using fopen
Hey all, I'm trying to write a sort function but am having trouble figuring
I didn't write this code and I'm having trouble figuring out why I'm getting
I'm having trouble figuring out how much I can adjust the imageView property of
im having trouble figuring out how to bind mouseout() to my entire nav bar
I've having trouble figuring out how to select based on a list in a
I'm having trouble figuring out how to filter a queryset appropriately for the following
I'm having trouble figuring out how to get my navigation property to update with
I am having trouble figuring out what the elements are in parsed xml input,
I am having trouble figuring out what is causing the click event to fire

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.