# Order by child

**URL:** <https://www.ruby-forum.com/t/order-by-child/179004>\
**Category:** Rails\
**Created:** [November 15, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/order-by-child/179004 "2009-11-15T14:02:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pubclub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3d9bf3/32.png) [@pubclub](https://www.ruby-forum.com/u/pubclub)\
**Post date:** [November 15, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/order-by-child/179004/1 "2009-11-15T14:02:29Z")

</div>

Hello,

I have a Product model with a has\_many relationship towards a Order  
model. What i am trying to get is a list of all products, ordered by the  
most recent Order.

For example:

Product “Old Book” was last sold on 11 November  
Product “Camera Bag” was last sold on 14 November

“Old book” will be the first in the list and “Camera Bag” the second.

Regards,

Vincent

---

<div class="post-metadata">

**Author:** ![pubclub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3d9bf3/32.png) [@pubclub](https://www.ruby-forum.com/u/pubclub)\
**Post date:** [November 15, 2009, 3:04pm UTC](https://www.ruby-forum.com/t/order-by-child/179004/2 "2009-11-15T15:04:04Z")

</div>

Vincent B. wrote:

> Hello,
> 
> I have a Product model with a has\_many relationship towards a Order  
> model. What i am trying to get is a list of all products, ordered by the  
> most recent Order.
> 
> For example:
> 
> Product “Old Book” was last sold on 11 November  
> Product “Camera Bag” was last sold on 14 November
> 
> “Old book” will be the first in the list and “Camera Bag” the second.

Join the tables and use an appropriate order clause.

> Regards,
> 
> Vincent

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pubclub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3d9bf3/32.png) [@pubclub](https://www.ruby-forum.com/u/pubclub)\
**Post date:** [November 15, 2009, 3:18pm UTC](https://www.ruby-forum.com/t/order-by-child/179004/3 "2009-11-15T15:18:28Z")

</div>

I also want the products without an order. Wouldn’t that leave them out?

---

<div class="post-metadata">

**Author:** ![pubclub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3d9bf3/32.png) [@pubclub](https://www.ruby-forum.com/u/pubclub)\
**Post date:** [November 15, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/order-by-child/179004/4 "2009-11-15T16:27:13Z")

</div>

[Please quote when replying. It makes the discussion easier to follow.]

Vincent B. wrote:

> I also want the products without an order. Wouldn’t that leave them out?

Not if you use a left outer join.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pubclub](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3d9bf3/32.png) [@pubclub](https://www.ruby-forum.com/u/pubclub)\
**Post date:** [November 16, 2009, 11:19am UTC](https://www.ruby-forum.com/t/order-by-child/179004/5 "2009-11-16T11:19:55Z")

</div>

Vincent B. wrote:

> Hello,
> 
> I have a Product model with a has\_many relationship towards a Order  
> model. What i am trying to get is a list of all products, ordered by the  
> most recent Order.
> 
> For example:
> 
> Product “Old Book” was last sold on 11 November  
> Product “Camera Bag” was last sold on 14 November
> 
> “Old book” will be the first in the list and “Camera Bag” the second.
> 
> Regards,
> 
> Vincent

For anyone trying to find the end result. This is what I came up with

@products = Product.find(:all, :joins =\> "LEFT OUTER JOIN orders u ON  
u.product\_id = products.id ", :order =\> “u.created\_at DESC”, :group =\>  
“products.id”)
