# Rails3 help with query

**URL:** <https://www.ruby-forum.com/t/rails3-help-with-query/190763>\
**Category:** Rails\
**Created:** [June 27, 2010, 4:40am UTC](https://www.ruby-forum.com/t/rails3-help-with-query/190763 "2010-06-27T04:40:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [June 27, 2010, 4:40am UTC](https://www.ruby-forum.com/t/rails3-help-with-query/190763/1 "2010-06-27T04:40:08Z")

</div>

No response on another rails3 post (rails3 finders, rails3 gure’s and  
MVC police), but I’ll try another one.

I added a preference to a golf group score system that is similar to a  
USGA golf handicap. Lets say I have a table Rounds that contains the  
attributes “score” and “date” and some foreign keys. There are also  
some preferences in a Group table. I have a function in the model  
“compute\_tee\_quota” that calculates the quota (or handicap) based on  
records in Round.

In English what needs to be accomplished is:  
get the last 20 rounds for a member (or less if not 20)  
get the maximum date from that selection (last\_played)  
order the selection by score DESC and limit the selection to 10  
(best 10 scores - or less)  
reorder that selection by date DESC  
do some calculation and return the quota, last played and array  
of last 10 scores

Rails3 finders can’t be chained because of the three orders. Chaining  
them results in order date DESC, order score DESC, order date DESC

My kludge is:

```
  therounds = Round.where(:member_id => self.id, :tee_id =>

```

tee).order(“date DESC”).limit(group.round\_limit)  
last\_played = therounds.size \> 0 ? therounds.first.date : nil  
theroundids = therounds.select(:id).all  
therounds = Round.where(“id in  
(?)”,theroundids).order(“points\_pulled DESC”).limit((group.quota\_limit

- 1))  
theroundids = therounds.select(:id).all  
therounds = Round.where(“id in (?)”,theroundids).order(“date  
DESC”)  
do the calculations

This works, but maybe there is a better way? The last order is to get  
a list of scores used + 1 that shows what will be dropped off the next  
time a higher score is entered.

Steve A.
