Forum: Ruby on Rails After Two Queries Are Returned Merge Them and Order Them

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ba79eb31ceaef95e9e52f5e5e4ec6d02?d=identicon&s=25 John Kopanas (Guest)
on 2006-12-29 22:03
(Received via mailing list)
I am using mySQL.  I run two different finds and once I get them both
back I want to merge the results and order them?  What is the best way
to go about this?

Thanks for your help! :-)

John Kopanas
3299251223f55753ec84719de39e6e53?d=identicon&s=25 Jon Garvin (Guest)
on 2006-12-29 22:13
(Received via mailing list)
Assuming that you're not mixing results of two different classes (apples
and oranges)..

@all_apples = @first_apples + @second_apples
@sorted_apples = @all_apples.sort { |a,b| a.sortable_value <=>
b.sortable_value }

But, I would strongly recommend investigating the possibility of doing
it all in one sql call.  You'll find it much faster, especially if
you're dealing with a ton of apples.  find_by_sql may be your only
option in that regard, but it might be worth it.  If you're only dealing
with a bushel of apples each time, then the above may be sufficient.
This topic is locked and can not be replied to.