Forum: Ruby on Rails How to Sort by a column from another table

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2eb32b43f162fcbc1dc52c8576132931?d=identicon&s=25 Sunny Bak (sunfish)
on 2009-04-02 03:10
I need to create a list of users sorted by division.  How do I sort User
table by Division name?  I can only get it to sorted by division_id.

Part of my code is shown below:

sort = case params['sort']
           when "id"  then "id ASC"
           when "email" then "email ASC"
           when "division_id" then "division_id ASC"


 @users = User.find(:all, :order => sort)
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-04-02 04:36
(Received via mailing list)
>
>
> @users = User.find(:all, :order => sort)

@users = User.find(:all, :include => :division, :order =>
'divisions.name')

(assuming Division has a 'name' field)
This topic is locked and can not be replied to.