:include and sorting

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

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.

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.

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