# Has\_many :through with :order?

**URL:** <https://www.ruby-forum.com/t/has-many-through-with-order/90538>\
**Category:** Rails\
**Created:** [March 29, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/has-many-through-with-order/90538 "2007-03-29T19:23:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [March 29, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/has-many-through-with-order/90538/1 "2007-03-29T19:23:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [March 29, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/has-many-through-with-order/90538/2 "2007-03-29T19:39:46Z")

</div>

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](http://www.dummied.org)

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [March 29, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/has-many-through-with-order/90538/3 "2007-03-29T22:41:02Z")

</div>

> > 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?
