# Sorting on a related STI-field

**URL:** <https://www.ruby-forum.com/t/sorting-on-a-related-sti-field/127001>\
**Category:** Rails\
**Created:** [January 24, 2008, 1:31am UTC](https://www.ruby-forum.com/t/sorting-on-a-related-sti-field/127001 "2008-01-24T01:31:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [January 24, 2008, 1:31am UTC](https://www.ruby-forum.com/t/sorting-on-a-related-sti-field/127001/1 "2008-01-24T01:31:03Z")

</div>

I have the following models:  
Event  
name  
location  
customer\_id  
with relation  
belongs\_to :customer

Person  
name  
address  
city  
type

Customer \< Person  
has\_many :events

Now I want to have a list, sorted on the name of the customer.

This doesnâ€™t work  
Event.find(:all, :include =\> :customer, :order =\> â€˜customer.nameâ€™)

Or isnâ€™t this possible at all, just back to plain sql?

Greetz, Adrie

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [January 24, 2008, 3:21am UTC](https://www.ruby-forum.com/t/sorting-on-a-related-sti-field/127001/2 "2008-01-24T03:21:00Z")

</div>

sure you can write in plain sql using find\_by\_sql method. but it is an  
easy query and it would easier doing it directly in rails code.  
I’m not sure i understand what you want maybe it is :  
Costumer.find :all, :include =\> :events, :order =\> ‘customers.name’

and a question: maybe you made an errors by typing but why you wrote  
customer (singular) in ‘customer.name’ ?

On Jan 24, 1:31 am, Adrie D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [January 24, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/sorting-on-a-related-sti-field/127001/3 "2008-01-24T13:48:50Z")

</div>

Jean-sÃ©bastien Jney wrote:

> Costumer.find :all, :include =\> :events, :order =\> ‘customers.name’

Jean,

I want the Events (a selection on event.starttime) listed, but sorted  
bij de Customer’s name. Not all Events have a customer, so your query  
went give them.

That why i tried:  
Event.find(:all, :include =\> :customer, :order =\> ‘customer.name’)

Anyway, thanks for your reply  
Adrie

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [January 24, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/sorting-on-a-related-sti-field/127001/4 "2008-01-24T14:08:52Z")

</div>

On 24 Jan 2008, at 12:48, Adrie D. wrote:

> That why i tried:  
> Event.find(:all, :include =\> :customer, :order =\> ‘customer.name’)

just add a condition: ‘customers.id IS NOT NULL’ (for mysql, other dbs  
will have something equivalent’).  
There’s also :joins instead of :include (which does mostly the same  
joining, but doesn’t actually instantiate the customer objects)

Fred

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [January 24, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/sorting-on-a-related-sti-field/127001/5 "2008-01-24T15:52:04Z")

</div>

Frederick C. wrote:

> just add a condition: ‘customers.id IS NOT NULL’ (for mysql, other dbs  
> will have something equivalent’).  
> There’s also :joins instead of :include (which does mostly the same  
> joining, but doesn’t actually instantiate the customer objects)
> 
> Fred

Fred,

I worked it out, this is the way:

Event.find(:all, :include =\> :customer, :order =\> ‘people.name’)

The NULL clause doesn’t seem necessary, just use the plural name of the  
table, not the model.

Thanks for the support  
Adrie
