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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:58:48+00:00 2026-05-24T21:58:48+00:00

My setup: Rails 3.0.9, Ruby 1.9.2 Here are my models class Project has_many :tasks

  • 0

My setup: Rails 3.0.9, Ruby 1.9.2

Here are my models

class Project
  has_many :tasks
  has_many :photos

class Task
  belongs_to :project

class Photos
  belongs_to :project

What I need is to be able to return a result set sorted first by project name (ASC) and then interleave tasks and photos sorted by date (DESC). Here’s a sample output of the order I need

project: install new roof
  photo: ...(added on 7/15/11)
  task: ...(added on 7/10/11)
  task: ...(added on 7/1/11)

project: repair bathtub
  task: ... (added on 8/21/11)
  photo: ... (added on 8/20/11)
  photo: ...(added on 8/17/11)
  task: ... (added on 8/15/11)

I prefer to get the entire result set from ActiveRecord and then sort it in Ruby but unsure how to go about it, any insights will be much appreciated. 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-05-24T21:58:49+00:00Added an answer on May 24, 2026 at 9:58 pm

    How about something like this? Grab all the objects, eagerly loaded from the DB, then sort them with Array#sort!.

    # First, grab all projects and their task/photos from the DB
    projects = Project.includes(:tasks, :photos).order("project_name ASC")
    
    # Now, iterate over projects, combine tasks/photos, sort by date
    projects.each do |p|
      tasks_and_photos = p.tasks + p.photos
      tasks_and_photos.sort! { |x,y| y.created_at <=> x.created_at }
      # now do something with the tasks and photos!
    end
    

    Adding a note to this, if you didn’t want to use the created_at attribute for your sort condition, all you would need to do is ensure that both Photo and Task have some attribute with a common name, such as date_added or whatever. As long as there is something with a common name to sort by, you’re good to go.

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

Sidebar

Related Questions

Here's the setup: New Rails app, then put this test_rake.rake in lib/tasks: task :testclass
I'm trying to get Ruby on Rails setup on my new eeeubuntu install and
I've been trying to get setup with Ruby on Rails today, but I think
With the 2.3.x+ rails feature of nested models, I think I need to have
I am creating a site in Ruby on Rails, I have two models a
Hey guys I have a ruby on rails app with a before filter setup
Now this is my first question here. I'm trying to install Ruby On Rails
I'm new to rails and working my way into the tutorial here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book I've
My setup: Rails 2.3.10, Ruby 1.8.7 I have a rather complicated set of relationships
My setup: Rails 2.3.10, Ruby 1.8.7 I have experimented, without success, with trying to

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.