Rails3 help with query

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
    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.