Forum: Ruby on Rails doing a SQL sum and getting the result into rails

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
744b6438d1457d332d1a8d431cc2b641?d=identicon&s=25 (Guest)
on 2007-04-20 09:19
(Received via mailing list)

I am trying to execute the following SQL code and have the result
returned to my application for further proessing...

"select SUM(action) as 'sum' from approvals where upload_id = ?",

I have tried this in a Approval.find_by_sql also as a
Approval.connection.execute, but I can't seem to get the data back
into rails... How can I access the returned data, which should be a
single number? I tried @points.sum, @points, etc.  I want to do some
numerical comparisons on this, so I need the number, and just the

Oh, and `action` can be any number from -3 to 3, so I can't just do a

Any ideas?

153868096910b6bca47e2a9212c2df56?d=identicon&s=25 Frederick Cheung (fcheung)
on 2007-04-20 11:04
Approval.sum 'action', :conditions => ['upload_id = ?',]
should do the trick.

If you go down the road of writing out the sql by hand (not necessary
here, but things aren't always as simple) then you can use

This topic is locked and can not be replied to.