# How to sum returned rows from a model using inject?

**URL:** <https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291>\
**Category:** Rails\
**Created:** [July 18, 2009, 6:50pm UTC](https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291 "2009-07-18T18:50:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 18, 2009, 6:50pm UTC](https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291/1 "2009-07-18T18:50:06Z")

</div>

Here’s my current loop:

update\_total = model.find(:all)  
update\_total.each do |row|

val = ((row.totoff + row.rushoff + row.passoff + row.scoroff +  
row.rzonoff + row.fumlost + row.passhint + row.tolost + row.sacksall +  
row.tackflossall + row.passeff + row.firdwns + row.thrdwncon +  
row.fthdwncon)/14.0).round\_to(4)

row.totals = val  
row.save  
puts “Team = #{row.team.name} and total val = #{val}.”  
end

This of course looks sloppy. I thought I could use something similar:

val = row.inject(0){|sum,item| sum + item}

… but I can’t figure out how to use this without it adding specific  
columns, namely…

team\_id, totals, compiled\_on, created\_at, updated\_at

… these 5 columns should be exempt from sums…

It’s not overly important and I can work around it. I guess I just want  
to keep my code as clean as possible and didn’t know if there is a more  
preferred way to sum up a row’s totals in rails…

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 18, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291/2 "2009-07-18T20:41:03Z")

</div>

Ã„lphÃ¤ BlÃ¼Ã« wrote:  
[…]

> … but I can’t figure out how to use this without it adding specific  
> columns, namely…
> 
> team\_id, totals, compiled\_on, created\_at, updated\_at
> 
> … these 5 columns should be exempt from sums.

How about using [:column1, :column2, …] - [:team\_id, :totals, …] ?  
Remember, Array#- actually works in Ruby.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 18, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291/3 "2009-07-18T21:38:13Z")

</div>

Thanks Marnen,

I had a feeling it was going to be simpler than I was trying to make it  
out to be. Appreciate the info…

I think I’ve learned more about arrays and hashes in ruby this week over  
anything, which is a good thing.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 19, 2009, 5:20am UTC](https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291/4 "2009-07-19T05:20:58Z")

</div>

Thanks for the advice Craig. I’m always looking for better way to do  
things. Much appreciated.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 19, 2009, 4:50am UTC](https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291/5 "2009-07-19T04:50:34Z")

</div>

On Sat, Jul 18, 2009 at 12:50 PM, Ã„lphÃ¤ BlÃ¼Ã« \<  
removed\_email\_address@domain.invalid\> wrote:

> Here’s my current loop:
> 
> update\_total = model.find(:all)  
> update\_total.each do |row|
> 
> val = ((row.totoff + row.rushoff + row.passoff + row.scoroff +  
> row.rzonoff + row.fumlost + row.passhint + row.tolost + row.sacksall +  
> row.tackflossall + row.passeff + row.firdwns + row.thrdwncon +  
> row.fthdwncon)/14.0).round\_to(4)

Instead of querying the model for all of this data, I recommend  
encapsulating this calculation in the model itself. Also, I’d give  
meaningful names to 14.0 and 4 to clearly communicate what purpose they  
serve in the calculation.

Then your loop could be:

Model.all.each do |model|  
model.recalculate\_total  
model.save!  
puts …  
end

–  
Craig D.  
Mutually Human Software

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com/)**
>
> Technology Made for Humans by humans Data Analytics Data Analytics Data has become a critical part of business for most organizations. We work with you to ensure you have confidence in your data, can properly access it, and present it in a way that...

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 19, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291/6 "2009-07-19T15:47:07Z")

</div>

Ã„lphÃ¤ BlÃ¼Ã« wrote:

> Hi Craig,
> 
> Did you mean something like this?
> 
> Both methods are in the model…
> 
> def recalculate\_totals(row)  
> […]

You don’t need an argument on this method, since the row is already  
accessible as self.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 19, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291/7 "2009-07-19T15:15:47Z")

</div>

Hi Craig,

Did you mean something like this?

Both methods are in the model…

def recalculate\_totals(row)  
fields = 5.0  
row.totals = ((row.kickret + row.puntret + row.netpunt +  
row.kickretdef + row.puntretdef) / fields).round\_to(4)  
end

model.compiled\_this\_week.all.each do |row|  
row.recalculate\_totals(row)  
row.save!  
puts “Team = #{row.team.name} and total val = #{row.totals}.”  
end

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 19, 2009, 4:35pm UTC](https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291/8 "2009-07-19T16:35:06Z")

</div>

Hi,  
Yes, and Marnen’s observation in a later message about not needing to  
pass  
row is spot on.

Regards,  
Craig

–  
Craig D.  
Mutually Human Software

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com/)**
>
> Technology Made for Humans by humans Data Analytics Data Analytics Data has become a critical part of business for most organizations. We work with you to ensure you have confidence in your data, can properly access it, and present it in a way that...

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 19, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/how-to-sum-returned-rows-from-a-model-using-inject/172291/9 "2009-07-19T21:49:40Z")

</div>

Thanks guys.

I changed it to:

def recalculate\_totals  
fields = 5.0  
self.totals = ((self.kickret + self.puntret + self.netpunt +  
self.kickretdef + self.puntretdef) / fields).round\_to(4)  
end

and…

model.compiled\_this\_week.all.each do |row|  
row.recalculate\_totals  
row.save!  
puts “Team = #{row.team.name} and total val = #{row.totals}.”  
end
