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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:55:01+00:00 2026-05-16T03:55:01+00:00

I am trying to do a find which orders results by their house name

  • 0

I am trying to do a find which orders results by their house name and then by the customer’s last name.

Customer.find(:all, 
    :conditions =>['customers.id IN (?)', intersection], 
    :joins => 'JOIN histories ON histories.customer_id = customers.id
     JOIN houses ON histories.house_id = houses.id',
    :order => "houses.name ASC, customers.last_name ASC",
    :select => "customers.*, histories.job_title, houses.name"
)

My problem is this will return every history related to each customer.

if I add AND histories.finish_date IS NULL
This will prevent every history for the selected customer being returned but it will also stop customers in the intersection who have no history or a finish_date set from being returned.

Basically I need every customer in the intersection returned once with there current house name(if they have one) and then ordered by their house name and then their last name.

So is there a way of doing this?

Here is an example

customer

id  last_name
1     franks
2     doors
3     greens

histories

id    finish_date      house_id     customer_id
1        NULL             1             1
2        NULL             2             2
3        11/03/10         2             1
4        22/04/09         1             2

NULL = current house

houses

id name
1    a
2    b

Results

intersection = 1,2,3

last_name       house     
franks            a
doors             b
greens            NULL

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-16T03:55:02+00:00Added an answer on May 16, 2026 at 3:55 am

    I think you need to use outer joins.

    For example, this should work:

    Customer.find(:all, 
      :conditions =>['customers.id IN (?) and histories.finish_date is null', intersection], 
      :joins => 'LEFT OUTER JOIN histories ON histories.customer_id = customers.id
        LEFT OUTER JOIN houses ON histories.house_id = houses.id',
      :order => "houses.name ASC, customers.last_name ASC",
      :select => "customers.*, histories.job_title, houses.name"
    )
    

    If you’ve got an association between Customer and History and between History and House you should be able to do :include => [:histories => :house] instead of the :joins option.

    The only other thing is that the customers with no house will appear first in the list due to NULL being earlier in the order than a non-NULL value. You might want to try an order option like this :

    :order => 'isnull(houses.name), houses.name, customers.last_name'
    

    to achieve what you specified.

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

Sidebar

Related Questions

I have been messing around with Leaks trying to find which function is not
I am trying to find out which edges from a graph are bidirectional. Each
I'm trying to find rows which are within ____meters from the given point. THis
Within an asp.net MVC app, I'm trying to find rooms which have a guest/client,
I'm trying to find an algorithm which breaks the safe by typing the keys
I am trying to find some code which will help me to find out
I'm trying to find a MySQL query which will obtain the time that is
I'm trying to find out if there are any methods in Java which would
I'm trying to find the best way to update an object which could be
I'm trying to find an implementation of java.io.FileFilter which understands patterns like these: **/*.xml

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.