# Easiest way to order by field in joined table

**URL:** <https://www.ruby-forum.com/t/easiest-way-to-order-by-field-in-joined-table/91548>\
**Category:** Rails\
**Created:** [April 10, 2007, 10:14am UTC](https://www.ruby-forum.com/t/easiest-way-to-order-by-field-in-joined-table/91548 "2007-04-10T10:14:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [April 10, 2007, 10:14am UTC](https://www.ruby-forum.com/t/easiest-way-to-order-by-field-in-joined-table/91548/1 "2007-04-10T10:14:48Z")

</div>

hey there -

given a simple table like this:  
orders.id  
orders.client\_id  
orders.supplier\_id  
orders.date\_ordered

I create and index view that has in it something like this:

\<%= @orders.id %\> \<%= @orders.client\_name %\> \<%= @orders.supplier\_name %\> \<%= @orders.date\_ordered %\>

Ultimately, I’m wanting to provide an ‘index’ page, that can be  
resorted on a column of the users choice (i.e.id, client\_name,  
suplier\_name, date\_ordered)

So thats obviously easy for date\_ordered, and id  
i.e.  
Order.find(:all, :order=\>“date\_ordered”) for example

BUT to sort by the the client name or supplier name, data which are  
obviously in a joined table - it gets curlier. Coming from an sql  
background, i’d assume it should be very very simple, but activerecord  
seems to obscure something simple and make it a guessing game of  
syntax - ok - probably not a fair comment… can someone set me  
straight on this and show me how ?

thanks

glenn

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [April 10, 2007, 10:53am UTC](https://www.ruby-forum.com/t/easiest-way-to-order-by-field-in-joined-table/91548/2 "2007-04-10T10:53:54Z")

</div>

should just work like:

@orders = Order.find :all, :include =\> :suppliers, :order =\>  
“supplier.name DESC”

the value to pass to :order is just an SQL Fragment.  
in SQL you would do “ORDER BY supplier.name DESC”  
in AR you do :order =\> “supplier.name DESC”

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [April 10, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/easiest-way-to-order-by-field-in-joined-table/91548/3 "2007-04-10T12:23:17Z")

</div>

On Apr 10, 6:52 pm, “Thorsten” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> should just work like:
> 
> @orders = Order.find :all, :include =\> :suppliers, :order =\>  
> “supplier.name DESC”
> 
> the value to pass to :order is just an SQL Fragment.  
> in SQL you would do “ORDER BY supplier.name DESC”  
> in AR you do :order =\> “supplier.name DESC”

Beautiful… thanks - like I said, prob wasnt a fair comment 😉

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [April 10, 2007, 9:02pm UTC](https://www.ruby-forum.com/t/easiest-way-to-order-by-field-in-joined-table/91548/4 "2007-04-10T21:02:04Z")

</div>

> \<%= @orders.date\_ordered %\> which are obviously in a joined table - it gets curlier. Coming from an sql background, i'd assume it should be very very simple, but activerecord seems to obscure something simple and make it a guessing game of syntax - ok - probably not a fair comment... can someone set me straight on this and show me how ?

Try…

Order.find(:all, {:include=\>[:client, :supplier],  
:order=\>‘clients.name’})

- donald
