Hello, I was wondering if there was a method in Rails that returns the sum of a column. For example, I have a column called ''score'' and writing a SQL statement such a ''select sum(score) from table_name'' does return the sum of the values in the column. In the past (not too long ago being a newbie), I defined all sorts of methods only to discover that Rails (after reading Agile Web Dev) already had something much simpler (I don''t mind since it''s helping me learn Ruby along the way). My question is this, does Rails have any methods that can do this .. def self.total_points @points = self.find_by_sql("select sum(score) from scores") points = @points[0] #assigns first row points = points.attributes #assigns as hash points = points.values # pulls out the value points = points.to_s #converts to string points = points.to_i # converts to fixnum end Or am I wanting to be too concise. Also, if there isn''t a Rails way of condensing the above, is there any shortcut in Ruby. Thanks for any suggestions in advance! -- Posted via http://www.ruby-forum.com/.
Wilson Bilkovich
2006-Jan-07 06:14 UTC
[Rails] Using find_by_sql to get the sum of a column
On 1/6/06, richard downe <rmdowne@yahoo.com> wrote:> Hello, > > I was wondering if there was a method in Rails that returns the sum of a > column. For example, I have a column called ''score'' and writing a SQL > statement such a ''select sum(score) from table_name'' does return the sum > of the values in the column. In the past (not too long ago being a > newbie), I defined all sorts of methods only to discover that Rails > (after reading Agile Web Dev) already had something much simpler (I > don''t mind since it''s helping me learn Ruby along the way). > > My question is this, does Rails have any methods that can do this .. > > def self.total_points > @points = self.find_by_sql("select sum(score) from scores") > points = @points[0] #assigns first row > points = points.attributes #assigns as hash > points = points.values # pulls out the value > points = points.to_s #converts to string > points = points.to_i # converts to fixnum > end > > Or am I wanting to be too concise. Also, if there isn''t a Rails way of > condensing the above, is there any shortcut in Ruby. >class Score < ActiveRecord::Base def self.total_points connection.select_value("select sum(score) from scores").to_i end end
Alain Ravet
2006-Jan-07 10:35 UTC
[Rails] Re: Using find_by_sql to get the sum of a column
Richard As an alternative, have a look at the CalculationsPlugin; http://techno-weenie.net/blog/main/266/introducing-calculations http://techno-weenie.net/blog/code/269/more-on-activerecord-calculations (note: I haven''t used it yet) Alain
richard downe
2006-Jan-07 22:50 UTC
[Rails] Re: Using find_by_sql to get the sum of a column
Alain Ravet wrote:> Richard > > As an alternative, have a look at the CalculationsPlugin; > > http://techno-weenie.net/blog/main/266/introducing-calculations > > http://techno-weenie.net/blog/code/269/more-on-activerecord-calculations > > (note: I haven''t used it yet) > > > AlainThanks to both of you for the help! -- Posted via http://www.ruby-forum.com/.
On 1/7/06, richard downe <rmdowne@yahoo.com> wrote:> def self.total_points > @points = self.find_by_sql("select sum(score) from scores") > points = @points[0] #assigns first row > points = points.attributes #assigns as hash > points = points.values # pulls out the value > points = points.to_s #converts to string > points = points.to_i # converts to fixnum > end > > Or am I wanting to be too concise. Also, if there isn''t a Rails way of > condensing the above, is there any shortcut in Ruby.def self.total_points @points = self.count_by_sql("SELECT SUM(score) FROM scores") end and you''ll get a fixnum, non conversion needed !
> def self.total_points > @points = self.count_by_sql("SELECT SUM(score) FROM scores") > endIn calculations it''s: Score.calculate :sum, :score Calculations really come in handy when you bring in group and having clauses, however. -- rick http://techno-weenie.net
Damon Clinkscales
2006-Jan-08 23:51 UTC
[Rails] Re: Using find_by_sql to get the sum of a column
richard downe <rmdowne@...> writes:> <at> points = self.find_by_sql("select sum(score) from scores")The other two answers are perfectly great, but I have to add a third way which shows how to grab that value with find_by_sql. def self.total_points points = self.find_by_sql("select sum(score) as scoresum from scores") return points[0].scoresum.to_i end -damon http://damonclinkscales.com/
> The other two answers are perfectly great, but I have to add a third way which > shows how to grab that value with find_by_sql. > > def self.total_points > points = self.find_by_sql("select sum(score) as scoresum from scores") > return points[0].scoresum.to_i > endThe issue with that is, find_by_sql returns a model. Why not just do this then? def self.total_points connection.select_value("select sum(score) as scoresum from scores").to_i end -- rick http://techno-weenie.net
Damon Clinkscales
2006-Jan-09 02:49 UTC
[Rails] Re: Using find_by_sql to get the sum of a column
Rick Olson <technoweenie@...> writes:> The issue with that is, find_by_sql returns a model. Why not just do this > then? > > def self.total_points > connection.select_value("select sum(score) as scoresum from scores").to_i > end >Yep, if it''s just a single value, I agree. A lot of people forget that they can rename columns and computations with "AS" in their SQL as part of a larger query, so I mentioned it. -damon