# Sorting by a field of an associated class

**URL:** <https://www.ruby-forum.com/t/sorting-by-a-field-of-an-associated-class/122410>\
**Category:** Rails\
**Created:** [December 7, 2007, 12:12pm UTC](https://www.ruby-forum.com/t/sorting-by-a-field-of-an-associated-class/122410 "2007-12-07T12:12:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 7, 2007, 12:12pm UTC](https://www.ruby-forum.com/t/sorting-by-a-field-of-an-associated-class/122410/1 "2007-12-07T12:12:25Z")

</div>

I feel like i should know this but i’ve got the lurgy and am feeling a  
bit dumb…

If a User belongs\_to a Company, how do i find a list of users, ordered  
by user.company.name?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 7, 2007, 12:26pm UTC](https://www.ruby-forum.com/t/sorting-by-a-field-of-an-associated-class/122410/2 "2007-12-07T12:26:25Z")

</div>

You can write a method like following in the user.rb

def \<=\>(user)  
self.company.name \<=\> user.company.name  
end

and use the sort method of array to sort the users. for example,  
users.sort

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 7, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/sorting-by-a-field-of-an-associated-class/122410/3 "2007-12-07T12:28:51Z")

</div>

If you are using user.find method then use the following symbols in the  
find method :include =\> ‘company’ :order =\> ‘company.name’

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 7, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/sorting-by-a-field-of-an-associated-class/122410/4 "2007-12-07T12:41:21Z")

</div>

Ayyanar Aswathaman wrote:

> If you are using user.find method then use the following symbols in the  
> find method :include =\> ‘company’ :order =\> ‘company.name’

nice one, thanks!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 7, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/sorting-by-a-field-of-an-associated-class/122410/5 "2007-12-07T13:21:48Z")

</div>

Max W. wrote:

> Ayyanar Aswathaman wrote:
> 
> > If you are using user.find method then use the following symbols in the  
> > find method :include =\> ‘company’ :order =\> ‘company.name’
> 
> nice one, thanks!

Actually to get this to work i had to change the value of :order to  
:order =\> ‘companies.name’, ie to match the name of the db table.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 7, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/sorting-by-a-field-of-an-associated-class/122410/6 "2007-12-07T14:42:50Z")

</div>

Max :

> Actually to get this to work i had to change the value of :order to  
> :order =\> ‘companies.name’, ie to match the name of the db table.

You can use :order =\> “#{Company.table\_name}.name”  
to avoid thinking about the real (eventually legacy) name of the db  
table.

– Jean-François.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [December 7, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/sorting-by-a-field-of-an-associated-class/122410/7 "2007-12-07T14:44:50Z")

</div>

Jean-FranÃ§ois TrÃ¢n wrote:

> Max :
> 
> > Actually to get this to work i had to change the value of :order to  
> > :order =\> ‘companies.name’, ie to match the name of the db table.
> 
> You can use :order =\> “#{Company.table\_name}.name”  
> to avoid thinking about the real (eventually legacy) name of the db  
> table.
> 
> – Jean-Franï¿½ois.

Thanks - that will be useful in few situations actually 🙂
