# :include and sorting

**URL:** <https://www.ruby-forum.com/t/include-and-sorting/107673>\
**Category:** Rails\
**Created:** [June 25, 2007, 7:01am UTC](https://www.ruby-forum.com/t/include-and-sorting/107673 "2007-06-25T07:01:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [June 25, 2007, 7:01am UTC](https://www.ruby-forum.com/t/include-and-sorting/107673/1 "2007-06-25T07:01:51Z")

</div>

I added an include to a find statement in order to preload data rather  
than using lazy loading  
eg.Client.find(params[:id],:include=\>[:contacts, :etc,:etc])

I have a sort clause in the Client model eg  
has\_many :contacts, :order=\>“contact\_date”

The sort works fine when listing Client.contacts in a view, but as  
soon as I do the include to preload the sort order is lost.

Is there any simple way around this apart from removing the include?

Cheers  
George

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [June 25, 2007, 11:36am UTC](https://www.ruby-forum.com/t/include-and-sorting/107673/2 "2007-06-25T11:36:31Z")

</div>

You might have to sort the contacts after you’ve got them using  
Array#sort. I’m not sure if MySQL allows for the sorting of joined  
rows. I’ve googled but I don’t see anything.

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [June 25, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/include-and-sorting/107673/3 "2007-06-25T20:26:21Z")

</div>

giorgio wrote:

> Is there any simple way around this apart from removing the include?  
> Client.find params[:id], :include=\>[:contacts, :etc, :etc],  
> :order =\> ‘contacts.contact\_date’

should work.

–  
We develop, watch us RoR, in numbers too big to ignore.
