Sorting on a related STI-field

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

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. [email protected]

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs