Forum: Ruby on Rails sum enumeration with LIMIT

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
67c5f4399394a68f8712e8ab25831789?d=identicon&s=25 yajiri (Guest)
on 2008-12-07 20:38
(Received via mailing list)
Hi everyone.

I'm trying to SUM the places of the first 5 finishers in a Cross
Country running event.  In a Cross Country race the team score is
based upon the sum of the top 5 runners on your team.  The lowest team
score at the competition wins.

I have EVENTS(the race), PARTICIPANTS(the runner), and
EVENT_PARTICIPANTS(a runner in a race).

Within event_participant I have this code, which doesn't work as it
adds places beyond the 5th runner.

  def self.score_5
    self.calculate(:sum, :finish_place, :order
=> :finish_place, :limit => 5)
  end

It appears that the LIMIT is ignored or is applied after the
summation.

Any suggestions?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-07 20:49
(Received via mailing list)
On 7 Dec 2008, at 19:37, yajiri wrote:

>
>
It's applied after (so if you did select sum(foo) from bars limit 5
then you'd only get 5 rows back).
Something like
SELECT sum(finish_place) from
  (SELECT * from some_table order by finish_place limit 5) as sub_select

should do it.

Fred
67c5f4399394a68f8712e8ab25831789?d=identicon&s=25 yajiri (Guest)
on 2008-12-07 21:02
(Received via mailing list)
Are us suggesting find_by_sql?

On Dec 7, 1:48 pm, Frederick Cheung <frederick.che...@gmail.com>
67c5f4399394a68f8712e8ab25831789?d=identicon&s=25 yajiri (Guest)
on 2008-12-08 00:07
(Received via mailing list)
I'd like to know if there is a way to use ActiveRecord to do this.

Thx
This topic is locked and can not be replied to.