# Sorting a has\_many collection

**URL:** <https://www.ruby-forum.com/t/sorting-a-has-many-collection/159843>\
**Category:** Rails\
**Created:** [February 9, 2009, 11:15am UTC](https://www.ruby-forum.com/t/sorting-a-has-many-collection/159843 "2009-02-09T11:15:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 9, 2009, 11:15am UTC](https://www.ruby-forum.com/t/sorting-a-has-many-collection/159843/1 "2009-02-09T11:15:34Z")

</div>

Can’t quite figure this one,

I have a School (has\_many reviews) and a Review (belongs\_to school).

I’m rendering my reviews per school like this “@School.reviews”

Which renders the “\_review.html.erb” multiple times for reviews of that  
school (works a treat).

But how do I control the order in which these are displayed (I’d like to  
reverse them basically most recent on top).

bb.

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 9, 2009, 11:46am UTC](https://www.ruby-forum.com/t/sorting-a-has-many-collection/159843/2 "2009-02-09T11:46:52Z")

</div>

this should work:  
@school.reviews.sort\_by { |cc| - cc.updated\_at.to\_i }

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 9, 2009, 11:56am UTC](https://www.ruby-forum.com/t/sorting-a-has-many-collection/159843/3 "2009-02-09T11:56:16Z")

</div>

class School \< AR  
has\_many :reviews, :order =\> ‘created\_at DESC’  
end

class Review \< AR  
belongs\_to :school  
end

If you use @school.reviews you’ll get the desired effect…

MaD  
escribió:\> this should work:

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 9, 2009, 12:11pm UTC](https://www.ruby-forum.com/t/sorting-a-has-many-collection/159843/4 "2009-02-09T12:11:24Z")

</div>

That’s ideal! thanks.

Follow up question, what if I want to override this order from time to  
time, say on some occasions have the reviews sorted by their last\_name  
column?

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 9, 2009, 6:02pm UTC](https://www.ruby-forum.com/t/sorting-a-has-many-collection/159843/5 "2009-02-09T18:02:11Z")

</div>

Bob,

You could also render it with jquery or some javascript library that  
enables you to write the set of reviews once to javascript and present  
and re-present in multiple ways as needed without more roundtrips to  
the server.

Al

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [February 9, 2009, 12:19pm UTC](https://www.ruby-forum.com/t/sorting-a-has-many-collection/159843/6 "2009-02-09T12:19:16Z")

</div>

You could use named\_scope to get more complexity. I think that’s a  
possibility.

> **[Locomotivation.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=locomotivation.com)**
>
> Extensive selection of high-quality domain names. Knowledgeable, friendly customer support.

bingo bob escribiÃ³:
