# Ordering across related tables

**URL:** <https://www.ruby-forum.com/t/ordering-across-related-tables/131740>\
**Category:** Rails\
**Created:** [March 12, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/ordering-across-related-tables/131740 "2008-03-12T22:04:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 12, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/ordering-across-related-tables/131740/1 "2008-03-12T22:04:23Z")

</div>

I have two related tables: groups and categories. In the models,

Group  
has\_many “categories”

Category  
belongs\_to :group

Category does have a column, “group\_id” as required.  
Both the groups and categories tables have a “sortorder” column which  
dictates the order the items are to be presented.

In my controller, I want to retrieve the Categories along with the  
information on the Group it belongs to. They should be retrieved in  
sortorder sequence with group having the highest precedence.

How can I specify a find statement with an :order\_by that specifies both  
Group.sortorder and Category.sortorder?

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 13, 2008, 2:47am UTC](https://www.ruby-forum.com/t/ordering-across-related-tables/131740/2 "2008-03-13T02:47:56Z")

</div>

Group.find(:all, :include=\>:categories, :order=\>‘groups.sortorder,  
categories.sortorder’)

On Mar 12, 5:04 pm, Michael S. \<rails-mailing-l…@andreas-
