Forum: Ruby on Rails has_many :through with :order?

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.
Rick (Guest)
on 2007-03-29 21:23
(Received via mailing list)
Let's say I have

class Student < ActiveRecord::Base
  has_many :classes_students
  has_many :classes, :through => :classes_students
end

I want the list of classes for each student to be sorted by the class
title.
I could get an array containing the classes and then sort it.  But, I
would like for the database to do the work.

Is there some combination of :include and :order that would cause
rails to ask for a sorted list?

I know this does not work:
  has_many :classes, :through => :classes_students, :order => 'title'

Any ideas?
Chris Vannoy (Guest)
on 2007-03-29 21:39
(Received via mailing list)
User.find(:all, :include => [:profile], :order => 'profiles.name ASC')

Seems to work for me ... not sure that it should, but it does.

Chris
http://www.dummied.org
Kim (Guest)
on 2007-03-30 00:41
(Received via mailing list)
> > I know this does not work:
> >   has_many :classes, :through => :classes_students, :order => 'title'

What do you mean this does not work?  It works for me. Does it not
sort it the way you want?
This topic is locked and can not be replied to.