# Using Order on Related Tables

**URL:** <https://www.ruby-forum.com/t/using-order-on-related-tables/145839>\
**Category:** Rails\
**Created:** [August 14, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/using-order-on-related-tables/145839 "2008-08-14T12:45:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MJohnH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/41988e/32.png) [@MJohnH](https://www.ruby-forum.com/u/MJohnH)\
**Post date:** [August 14, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/using-order-on-related-tables/145839/1 "2008-08-14T12:45:55Z")

</div>

I have tables related as below:  
class Partner \< ActiveRecord::Base

# :org\_name, :string

# amongst other things

has\_many :partner\_contacts  
end

class PartnerContact \< ActiveRecord::Base

# :partner\_id, :integer, :null =\> false

# :name, :string

# and other stuff

belongs\_to :partner  
end

I want to do a find on PartnerContact but order on Partner as follows:  
partner\_contacts = PartnerContact.find(:all, :order =\>  
“partner.org\_name, name”), which doesn’t work.

I can’t get my head around the syntax required to do this. Any help  
much appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![MJohnH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/41988e/32.png) [@MJohnH](https://www.ruby-forum.com/u/MJohnH)\
**Post date:** [August 14, 2008, 12:54pm UTC](https://www.ruby-forum.com/t/using-order-on-related-tables/145839/2 "2008-08-14T12:54:09Z")

</div>

On 14 Aug 2008, at 11:45, MJohnH wrote:

> # :name, :string
> 
> # and other stuff
> 
> belongs\_to :partner  
> end
> 
> I want to do a find on PartnerContact but order on Partner as follows:  
> partner\_contacts = PartnerContact.find(:all, :order =\>  
> “partner.org\_name, name”), which doesn’t work.
> 
> I can’t get my head around the syntax required to do this. Any help  
> much appreciated, thanks.  
> Use the :joins option to join the tables appropriately (:joins takes  
> either association names or an sql fragment)

Fred

---

<div class="post-metadata">

**Author:** ![MJohnH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/41988e/32.png) [@MJohnH](https://www.ruby-forum.com/u/MJohnH)\
**Post date:** [August 14, 2008, 12:54pm UTC](https://www.ruby-forum.com/t/using-order-on-related-tables/145839/3 "2008-08-14T12:54:44Z")

</div>

> I want to do a find on PartnerContact but order on Partner as follows:  
> partner\_contacts = PartnerContact.find(:all, :order =\>  
> “partner.org\_name, name”), which doesn’t work.

It’s  
partner\_contacts = PartnerContact.find(:all, :order =\>  
“partners.org\_name, name”, :include =\> [:partners])

the :include is needed to make that a joined query (I think :join  
would work too)  
and for “partners.org\_name, name” it’s important to use partners  
instead of partner,  
since this is for SQL and reflects the table name.

---

<div class="post-metadata">

**Author:** ![MJohnH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/41988e/32.png) [@MJohnH](https://www.ruby-forum.com/u/MJohnH)\
**Post date:** [August 14, 2008, 12:58pm UTC](https://www.ruby-forum.com/t/using-order-on-related-tables/145839/4 "2008-08-14T12:58:16Z")

</div>

On 14 Aug 2008, at 11:53, Thorsten Müller wrote:

> “partners.org\_name, name”, :include =\> [:partners])
> 
> the :include is needed to make that a joined query (I think :join  
> would work too)

Not only would :joins work, but it’s faster than :include if you don’t  
actually need to eager load the associations.

Fred

---

<div class="post-metadata">

**Author:** ![MJohnH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/41988e/32.png) [@MJohnH](https://www.ruby-forum.com/u/MJohnH)\
**Post date:** [August 14, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/using-order-on-related-tables/145839/5 "2008-08-14T13:07:41Z")

</div>

Many thanks, Fred and Thorsten.  
For completeness, in case anybody else finds this as useful as I do,  
the correct syntax is

partner\_contacts = PartnerContact.find(:all, :order =\>  
“partners.org\_name, name”, :include =\> [:partner])

so the include is singular but the reference in the order clause is  
plural.

Martin

On Aug 14, 11:57 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![MJohnH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/41988e/32.png) [@MJohnH](https://www.ruby-forum.com/u/MJohnH)\
**Post date:** [August 14, 2008, 1:27pm UTC](https://www.ruby-forum.com/t/using-order-on-related-tables/145839/6 "2008-08-14T13:27:36Z")

</div>

On 14 Aug 2008, at 12:07, Martin H. wrote:

> Many thanks, Fred and Thorsten.  
> For completeness, in case anybody else finds this as useful as I do,  
> the correct syntax is
> 
> partner\_contacts = PartnerContact.find(:all, :order =\>  
> “partners.org\_name, name”, :include =\> [:partner])

Seriously, if you don’t need the include, a :join is the better  
solution.

Fred
