# Sorting has\_many - via parent association

**URL:** <https://www.ruby-forum.com/t/sorting-has-many-via-parent-association/118907>\
**Category:** Rails\
**Created:** [November 1, 2007, 9:00am UTC](https://www.ruby-forum.com/t/sorting-has-many-via-parent-association/118907 "2007-11-01T09:00:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [November 1, 2007, 9:00am UTC](https://www.ruby-forum.com/t/sorting-has-many-via-parent-association/118907/1 "2007-11-01T09:00:29Z")

</div>

Hey,

I’m trying to sort an array result through a has many relationship, but  
am getting a little stuck. My bands model has\_many videos. I am trying  
to sort a list of videos by band.name and then video.name. I’ve seen  
that :include can be used for child sorting, is there, something that  
could work in reverse. Thanks!

My Models:

class Band \< ActiveRecord::Base  
has\_many :videos  
end

class Video \< ActiveRecord::Base  
belongs\_to :band  
end

-Mario

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [November 1, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/sorting-has-many-via-parent-association/118907/2 "2007-11-01T12:53:57Z")

</div>

On 11/1/07, Mario F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Band \< ActiveRecord::Base  
> has\_many :videos  
> end
> 
> class Video \< ActiveRecord::Base  
> belongs\_to :band  
> end

Video.find(:all, :include =\> :bands, :order =\> “bands.name,  
videos.name”)

should work, I think.

Note that the include option also means that all of the bands will be  
instantiated on this query, which may or may not be a good thing  
depending on what you’re going to do next.

I wrote a patch which is now in edge rails which extends the :joins  
option to take the same kind of values as includes. This lets you do:

Video.find(:all, :joins =\> :bands, :order =\> “bands.name, videos.name”)

Which does the same thing, but only instantiates the videos.

Be warned though, this patch may or may not make it into Rails 2.0  
since it’s very new.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![mgiorno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mgiorno](https://www.ruby-forum.com/u/mgiorno)\
**Post date:** [November 2, 2007, 7:08am UTC](https://www.ruby-forum.com/t/sorting-has-many-via-parent-association/118907/3 "2007-11-02T07:08:35Z")

</div>

Thanks for the help. I was a little reluctant to use edge rails. After  
some further review, I discover that the include statement needed to  
refer to the name of the association and not the name of the table. So  
you can sort by parent using the following:

#Controller Action Code  
Video.find(:all, :include =\> :band, :order =\> ‘bands.name ASC’ )

Built on these models Models:

class Band \< ActiveRecord::Base  
has\_many :videos  
end

class Video \< ActiveRecord::Base  
belongs\_to :band  
end
