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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:22:50+00:00 2026-05-25T12:22:50+00:00

The problem I’m having is like this: The model to sort is SchoolClass which

  • 0

The problem I’m having is like this: The model to sort is SchoolClass which has_many Students which in turn has_many Projects and each project has an end_date. I need to sort the SchoolClasses four ways: First by the earliest project end_date sort ascending and descending, and second by the latest project end_date sort ascending and descending. Does this make sense?

class SchoolClass < ActiveRecord::Base
  has_many :students
end

class Student < ActiveRecord::Base
  has_many :projects
  belongs_to :school_class
end

class Project < ActiveRecord::Base
  belongs_to :student
end

The only way I can think of doing it is very brute force and involves having a methods in the SchoolClass model that return the earliest and latest project dates for that instance like so:

students.collect(&:projects).flatten.select(&:end_date).sort.last

to find the latest project end_date for that class and then fetching out all the classes of the database and sorting them by that method. Surely this is just awful though, right? I would really like to find the rails way to get this ordering (with scopes maybe?). I thought something like SchoolClasses.joins(:students).joins(:projects).order('projects.end_date ASC') might work but that will crash rails (and looking at it now the logic is wrong anyway i think).

Any suggestions?

  • 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-25T12:22:51+00:00Added an answer on May 25, 2026 at 12:22 pm

    Try this:

    scs = SchoolClass.joins({:students => :projects}).
      select("school_classes.id, 
              MIN(projects.end_date) AS earliest_end_date,
              MAX(projects.end_date) AS latest_end_date").
      group("school_classes.id").
      order("earliest_end_date ASC")
    

    The objects in the scs array has following attributes:

    • id
    • earliest_end_date
    • latest_end_date

    If you need additional attributes you can do the following

    1) Add the additional attributes to the group and select methods

    2) Query the full SchoolClass object using the id

    3) Rewrite the query to use a nested JOIN

    scs = SchoolClass.joins(
    "JOIN (
       SELECT a.id, 
              MIN(c.end_date) AS earliest_end_date, 
              MAX(c.end_date) AS latest_end_date
        FROM  school_classes a
        JOIN  students b ON b.class_id = a.id
        JOIN  projects c ON c.student_id = b.id
        GROUP BY a.id
     ) d ON d.id = school_classes.id
    ").select("school_classes.*, 
               d.earliest_end_date AS earliest_end_date, 
               d.latest_end_date AS latest_end_date").
      order("earliest_end_date ASC")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem is like this: Suppose there are records in dataTable with FruitType as A
Problem: I have an address field from an Access database which has been converted
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem: Given a list of strings, find the substring which, if subtracted from the
Problem Language: C# 2.0 or later I would like to register context handlers to
Problem I have timestamped data, which I need to search based on the timestamp
Problem Description: We have a service which has applications for main mobile OS’s. We
PROBLEM: Here's the code: import matplotlib.pyplot as plt plt.bar([1,2],[5,4]) plt.title('this is a very long
PROBLEM: I have a Child class which uses DataContractSerialization and raises a Changed event
Problem: Visitors open the url website.com/?i=133r534|213213|12312312 but this url isn't valid anymore and they

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.