# :group usage

**URL:** <https://www.ruby-forum.com/t/group-usage/195399>\
**Category:** Rails\
**Created:** [September 22, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/group-usage/195399 "2010-09-22T18:03:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![icel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@icel](https://www.ruby-forum.com/u/icel)\
**Post date:** [September 22, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/group-usage/195399/1 "2010-09-22T18:03:19Z")

</div>

Hi,

I have the following code that is close to working the way I want it  
to:  
Dispatch.find(:all, :group =\> :order\_id, :conditions =\>  
[“ex\_factory\_date \< ?”, (Date.today + 1.month)], :order  
=\> :ex\_factory\_date)

So let me quickly explain what is going on. I have orders that have  
many dispatches. I am trying to make a report so that I find the  
earliest dispatch of an order and then not to show any more dispatches  
for that order. So my data is basically like this:  
Order 1 has 3 Dispatches with the following ex\_factory\_dates:  
Dispatch 1 = 12/10/2010  
Dispatch 2 = 13/10/2010  
Dispatch 3 = 20/10/2010

Order 2 has 2 Dispatches with the following ex\_factory\_dates:  
Dispatch 1 = 15/10/2010  
Dispatch 2 = 11/10/2010

So in my code I am using :group to ensure the order\_id is unique which  
it does. The only problem is that it seems to only select the last  
dispatch and I can not think of a way to make it select the dispatch  
with the earliest ex\_factory\_date. Hope that makes sense and any help  
much appreciated. I may be going about this the wrong way not sure.  
Thanks.
