# Trying to sort results of one-to-many relationship find

**URL:** <https://www.ruby-forum.com/t/trying-to-sort-results-of-one-to-many-relationship-find/157225>\
**Category:** Rails\
**Created:** [January 8, 2009, 5:10pm UTC](https://www.ruby-forum.com/t/trying-to-sort-results-of-one-to-many-relationship-find/157225 "2009-01-08T17:10:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [January 8, 2009, 5:10pm UTC](https://www.ruby-forum.com/t/trying-to-sort-results-of-one-to-many-relationship-find/157225/1 "2009-01-08T17:10:13Z")

</div>

Assuming I have two models:

Order “belongs\_to :payee”  
Payee “has\_many :orders”

In the orders table, I store the payee\_id.

If I want to pull all orders and display the list alphabetically by the  
payees last name, what would be the best way to do it?

Would I need to setup a true “join” table and then run my find on it? I  
can’t do this through the view using a sort plugin since that simply  
changes the finders SQL and doesn’t include the association logic needed  
here.

Since I’m accessing the payee’s name through the association  
“order.payee.last\_name” what is the best way to accomplish what I need  
or do I need to write some convoluted method that breaks down the  
recordset and orders it manually?

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [January 8, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/trying-to-sort-results-of-one-to-many-relationship-find/157225/2 "2009-01-08T18:57:19Z")

</div>

This is a plain association, no join table involved.

Try this in script/console and see if it solves your problem. The DB  
can easily take care of the sorting:

orders = Order.find(:all, :conditions =\> ‘…’, :include  
=\> :payee, :order =\> ‘payees.last\_name’)

Then,

orders.each { |o| puts o.payee.last\_name }

-H

On Jan 8, 11:10 am, Corey M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [January 8, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/trying-to-sort-results-of-one-to-many-relationship-find/157225/3 "2009-01-08T19:42:14Z")

</div>

Harold wrote:

> orders = Order.find(:all, :conditions =\> ‘…’, :include  
> =\> :payee, :order =\> ‘payees.last\_name’)

Awesome. The “:include” option wasn’t something I was aware you could  
use in a find for the model. I knew this could be handled using a nice  
rails-like convention without having to code some convoluted sorting  
method.

That makes life much easier. Thank you very much!
