# Order a find using a has\_many association

**URL:** <https://www.ruby-forum.com/t/order-a-find-using-a-has-many-association/91108>\
**Category:** Rails\
**Created:** [April 4, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/order-a-find-using-a-has-many-association/91108 "2007-04-04T19:38:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/order-a-find-using-a-has-many-association/91108/1 "2007-04-04T19:38:46Z")

</div>

I’m trying to order a find through a has\_many association. I have a  
model called SalesOrder. Each sales order has things like amount,  
description, etc. I use the following to get the Sales Orders:

@sales\_orders = SalesOrder.find(:all)

And I display them with:

\<%= link\_to sales\_order.id.to\_s, :action =\> 'show', :id =\> sales\_order.id %\> \<%= sales\_order.sold\_to %\> \<%= sales\_order.ship\_to %\> \<%= (sales\_order.rep.length \< 1 ? "&nbsp;" : sales\_order.rep) %\> \<%= sales\_order.description %\> \<%= sprintf("%#1.2f", sales\_order.amount) %\> \<%= sales\_order.ship\_date.strftime('%m/%d/%y') %\> \<%= sales\_order.latest\_history.nil? ? "&nbsp;" : sales\_order.latest\_history.route.name %\>

The problem is that this list is not in the order that I need it to  
be. I can order it by any column in the sales\_orders table, but I  
need to order it by latest\_history.route.name I don’t know how to  
order a query based on another table’s column. Is this even  
possible? Or is there another way to accomplish the same result? My  
model definitions are below:

class SalesOrder \< ActiveRecord::Base  
has\_many :histories  
has\_one :latest\_history,  
:class\_name =\> “History”,  
:order =\> “id desc”  
end

class History \< ActiveRecord::Base  
belongs\_to :route  
belongs\_to :sales\_order  
end

class Route \< ActiveRecord::Base  
has\_many :histories  
has\_and\_belongs\_to\_many :users

end

Thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/order-a-find-using-a-has-many-association/91108/2 "2007-04-04T19:45:47Z")

</div>

you might want to try this

@sales\_orders = SalesOrder.find(:all, :include =\> [:histories, :routes],  
:order =\> “routes.name”)

-Dutch

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/order-a-find-using-a-has-many-association/91108/3 "2007-04-04T20:21:51Z")

</div>

Ok,

I added the following to my SalesOrder model:

has\_many :routes, :through =\> :histories

Then I tried:

@sales\_orders = SalesOrder.find(:all, :include =\>  
[:histories, :routes], :order =\> ‘routes.name’)

That partially worked. It seemed to group together all sales orders  
where the route wasn’t blank. But they weren’t all grouped together  
by the same status. Here’s the order of the routes they were in:

Production Control  
Application Data Sheet Approval  
Manufacturing Engineering  
Released to Shop  
Manufacturing Engineering  
Manufacturing Engineering  
Manufacturing Engineering  
Manufacturing Engineering  
Project Engineering  
Released to Shop

Why weren’t the Manufacturing Engineering and Released to Shop routes  
all together?

Thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2007, 10:19pm UTC](https://www.ruby-forum.com/t/order-a-find-using-a-has-many-association/91108/4 "2007-04-04T22:19:08Z")

</div>

ah, I’m assuming you want to group sub items together, would you mind  
reponding back with you view code?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 4, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/order-a-find-using-a-has-many-association/91108/5 "2007-04-04T22:47:20Z")

</div>

Here you go:

\<% for sales\_order in @sales\_orders %\>

```
<td><%= sales_order.sold_to %></td>
<td><%= sales_order.ship_to %></td>
<td><%= sales_order.description %></td>
<td><%= sprintf("%#1.2f", sales_order.amount) %></td>
<td><%= sales_order.ship_date.strftime('%m/%d/%y') %></td>

<!-- this is the column of the table that's not sorting right -->
<td><%= sales_order.latest_history.nil? ? "&nbsp;" : 

```

sales\_order.latest\_history.route.name %\>
