# Ordering with eager loading

**URL:** <https://www.ruby-forum.com/t/ordering-with-eager-loading/153597>\
**Category:** Rails\
**Created:** [November 19, 2008, 7:31am UTC](https://www.ruby-forum.com/t/ordering-with-eager-loading/153597 "2008-11-19T07:31:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![felipegiotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/13edae/32.png) [@felipegiotto](https://www.ruby-forum.com/u/felipegiotto)\
**Post date:** [November 19, 2008, 7:31am UTC](https://www.ruby-forum.com/t/ordering-with-eager-loading/153597/1 "2008-11-19T07:31:42Z")

</div>

I’m trying to order my results by what I got with eager loading.

there is a bit of code about what i want

@study\_class = StudyClass.find(:all, :include =\> :teacher)

it is associated with an id, and i want to order my result by the  
teachers’  
name, which is just inside the teacher table that is loaded there.

is it possible or will i have to reorder it with ruby code?

thanks!

–  
gallois  
aka Felipe G.  
blog: www.gallois.com.br/blog  
fanglib homepage: www.gallois.com.br/fanglib

---

<div class="post-metadata">

**Author:** ![felipegiotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/13edae/32.png) [@felipegiotto](https://www.ruby-forum.com/u/felipegiotto)\
**Post date:** [November 19, 2008, 10:26am UTC](https://www.ruby-forum.com/t/ordering-with-eager-loading/153597/2 "2008-11-19T10:26:46Z")

</div>

> is it possible or will i have to reorder it with ruby code?

Can be done like that:

@study\_class = StudyClass.find(:all, :include =\> :teacher, :order =\>  
“teachers.name ASC”)

(since :order takes pure SQL, it must be the table name in plural  
as used in the database)

---

<div class="post-metadata">

**Author:** ![felipegiotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/13edae/32.png) [@felipegiotto](https://www.ruby-forum.com/u/felipegiotto)\
**Post date:** [November 19, 2008, 10:27am UTC](https://www.ruby-forum.com/t/ordering-with-eager-loading/153597/3 "2008-11-19T10:27:21Z")

</div>

On 19 Nov 2008, at 01:05, Felipe G. wrote:

> is it possible or will i have to reorder it with ruby code?  
> :order =\> ‘teachers.something’

Fred

---

<div class="post-metadata">

**Author:** ![felipegiotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/13edae/32.png) [@felipegiotto](https://www.ruby-forum.com/u/felipegiotto)\
**Post date:** [November 19, 2008, 12:58pm UTC](https://www.ruby-forum.com/t/ordering-with-eager-loading/153597/4 "2008-11-19T12:58:19Z")

</div>

On Wed, Nov 19, 2008 at 07:26, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > it is associated with an id, and i want to order my result by the  
> > thanks!

perfect! worked like a charm! thank you very much guys!

cheers!

–  
gallois  
aka Felipe G.  
blog: www.gallois.com.br/blog  
fanglib homepage: www.gallois.com.br/fanglib
