andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-20 07:19 UTC
doing a SQL sum and getting the result into rails
All, 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 = ?", @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 number. Oh, and `action` can be any number from -3 to 3, so I can''t just do a count... Any ideas? Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Apr-20 09:04 UTC
Re: doing a SQL sum and getting the result into rails
Approval.sum ''action'', :conditions => [''upload_id = ?'', @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 Approval.connection.select_values Fred -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---