how to sort on an object contained in a has_many association?

here are my models (only showing the associations that relate to this post):

class UpdateRequest < ActiveRecord::Base   belongs_to :user   belongs_to :contact end

class User < ActiveRecord::Base   has_many :update_requests, :dependent => :destroy end

so I want to be able to list UpdateRequests and sort them based on attributes of the referenced Contact in each

I cant, for instance, do the following for the User

       has_many :update_requests, :dependent => :destroy, :order => 'contacts.last_name asc'

It doesnt know about the 'contacts' table's columns

I can do this, right? How?

Thx

here are my models (only showing the associations that relate to this

post):

class UpdateRequest < ActiveRecord::Base

    belongs_to      :user

    belongs_to      :contact

end

class User < ActiveRecord::Base

    has_many        :update_requests, :dependent => :destroy

end

so I want to be able to list UpdateRequests and sort them based on

attributes of the referenced Contact in each

I cant, for instance, do the following for the User

   has_many :update_requests, :dependent => :destroy, :order =>

‘contacts.last_name asc’

It doesnt know about the ‘contacts’ table’s columns

I can do this, right? How?

Thx

How is this to be use? For example, are you displaying this information to the user within the

browser?

-Conrad

Yes, in a browser.

It's a page where I list these UpdateRequests showing, amongst other things, the name of the Contact for whom the UpdateRequest has been created. And I'd like to be able to have that list of UpdateRequests sorted on the Contact's last_name.

But, this is really also a general question about how to sort on (and how to get at) the attributes of an associated object from a object that joins objects (note that that's what an UpdateRequest does for me here).

Hi,

give this a try. Not sure it will work, haven’t tested it at all, but it might.

class User has_many :update_requests has_many :contacts, :through => :update_requests, :order => ‘last_name’

end

-Tim

The easiest way to get the contacts table into the mix is to use :include on the has_many:

has_many :update_requests, :dependent => :destroy, :include => :contact, :order => 'contacts.last_name asc'

I'm not 100% sure that this will actually sort the records the way you want, however. If it doesn't work, you'll probably need to build a named scope on UpdateRequest that sorts by contact last_name.

--Matt Jones

That did work, Matt. Thx!

This finally pushes me to learn more about :include, something I havent looked at much in a couple years of Rails work <sheepish grin>

Thx again to all

That did work, Matt. Thx!

This finally pushes me to learn more about :include, something I havent looked at much in a couple years of Rails work <sheepish grin>

I'd use :joins instead of include here - you'll actually be loading all those contact objects whenever you load that association, which probably isn't what you want.

Fred

:joins will work as well - but with the additional gotcha that it (by default) does an inner join, so UpdateRequests without an associated contact won't get loaded if you use the :joins => :contact syntax. Been bitten by this at least once myself...

Also, :include could be useful here anyway, as you're probably going to want to display at least some of the fields from Contact with the requests (else the sort will look really weird).

--Matt Jones

"you're probably going to want to display at least some of the fields from Contact"

exactly... I show the thing I'm sorting on (last_name) along with a number of other attr's