# How i add the sum in too this field

**URL:** <https://www.ruby-forum.com/t/how-i-add-the-sum-in-too-this-field/160766>\
**Category:** Rails\
**Created:** [February 19, 2009, 2:04pm UTC](https://www.ruby-forum.com/t/how-i-add-the-sum-in-too-this-field/160766 "2009-02-19T14:04:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![poloman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b4bc9f/32.png) [@poloman](https://www.ruby-forum.com/u/poloman)\
**Post date:** [February 19, 2009, 2:04pm UTC](https://www.ruby-forum.com/t/how-i-add-the-sum-in-too-this-field/160766/1 "2009-02-19T14:04:07Z")

</div>

Day work Start End Work time/Day

\<% for shift in @shifts %\>

 \<%= shift.date.strftime '%a' %\>Â \<%= shift.date.strftime '%d/%m/%Y' %\> \<%= time\_ampm shift.start %\> \<%= time\_ampm shift.end %\> \<%= (shift.end - shift.start)/100 %\>

now I want to add the sum of Work time/Day in to new field when i create

what is that command

---

<div class="post-metadata">

**Author:** ![poloman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b4bc9f/32.png) [@poloman](https://www.ruby-forum.com/u/poloman)\
**Post date:** [February 19, 2009, 2:30pm UTC](https://www.ruby-forum.com/t/how-i-add-the-sum-in-too-this-field/160766/2 "2009-02-19T14:30:19Z")

</div>

On Thu, Feb 19, 2009 at 5:17 AM, MaD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts sum  
> Also, you should be able to do the following:

sum = rows.inject(0) { | sum, row | sum + row.value }

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![poloman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b4bc9f/32.png) [@poloman](https://www.ruby-forum.com/u/poloman)\
**Post date:** [February 19, 2009, 2:18pm UTC](https://www.ruby-forum.com/t/how-i-add-the-sum-in-too-this-field/160766/3 "2009-02-19T14:18:27Z")

</div>

to be honest, i don’t really understand what you are trying to do, but  
if you just want to build a sum to put it into some field:  
initialize a variable with 0 (outside of your for-loop) and add any  
value you want to that variable.

sum = 0  
rows.each do |row|  
sum += row.value  
end  
puts sum

---

<div class="post-metadata">

**Author:** ![poloman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b4bc9f/32.png) [@poloman](https://www.ruby-forum.com/u/poloman)\
**Post date:** [February 19, 2009, 2:37pm UTC](https://www.ruby-forum.com/t/how-i-add-the-sum-in-too-this-field/160766/4 "2009-02-19T14:37:09Z")

</div>

from “\<%= (shift.end - shift.start)/100 %\>” mean work time per day

now i want to add the sum of Total work time i to new field

i’m newbie for ROR please tell me step by step , thank you .
