# Saving results of sum method into another column?

**URL:** <https://www.ruby-forum.com/t/saving-results-of-sum-method-into-another-column/157901>\
**Category:** Rails\
**Created:** [January 16, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/saving-results-of-sum-method-into-another-column/157901 "2009-01-16T13:07:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![herman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/eada6e/32.png) [@herman](https://www.ruby-forum.com/u/herman)\
**Post date:** [January 16, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/saving-results-of-sum-method-into-another-column/157901/1 "2009-01-16T13:07:27Z")

</div>

Basically what I’m trying to do is get the sum of 2 columns and save  
those results in another. I guess I’m looking to do something like  
this:  
a = Product.find(:all)  
b = Product.sum(“test + test2”, :group =\> “id”)  
a.test3 = b  
a.save

Obviously that doesn’t work, but I’m trying to achieve something like  
that. Any suggestions?

---

<div class="post-metadata">

**Author:** ![herman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/eada6e/32.png) [@herman](https://www.ruby-forum.com/u/herman)\
**Post date:** [January 16, 2009, 1:20pm UTC](https://www.ruby-forum.com/t/saving-results-of-sum-method-into-another-column/157901/2 "2009-01-16T13:20:24Z")

</div>

On 16 Jan 2009, at 10:19, Herman wrote:

> that. Any suggestions?

Product.update “test3 = test+test2” would probably do it.

Fred

---

<div class="post-metadata">

**Author:** ![herman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/eada6e/32.png) [@herman](https://www.ruby-forum.com/u/herman)\
**Post date:** [January 16, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/saving-results-of-sum-method-into-another-column/157901/3 "2009-01-16T19:42:05Z")

</div>

On 16 Jan 2009, at 16:34, Herman wrote:

> That didn’t work. I received: ArgumentError: wrong number of arguments  
> (1 for 2)
> 
> I tried something like this and it also didn’t work:  
> Product.update\_all(“test3”, :sum =\> “test”+“test2”, :group =\> “id”)

oops, typo, should be Product.update\_all “test3 = test+test2”

---

<div class="post-metadata">

**Author:** ![herman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/eada6e/32.png) [@herman](https://www.ruby-forum.com/u/herman)\
**Post date:** [January 16, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/saving-results-of-sum-method-into-another-column/157901/4 "2009-01-16T17:35:07Z")

</div>

That didn’t work. I received: ArgumentError: wrong number of arguments  
(1 for 2)

I tried something like this and it also didn’t work:  
Product.update\_all(“test3”, :sum =\> “test”+“test2”, :group =\> “id”)

I’d imagine I’m not that far off, but I can’t figure it out.

On Jan 16, 6:19 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![herman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/eada6e/32.png) [@herman](https://www.ruby-forum.com/u/herman)\
**Post date:** [January 17, 2009, 9:37pm UTC](https://www.ruby-forum.com/t/saving-results-of-sum-method-into-another-column/157901/5 "2009-01-17T21:37:48Z")

</div>

Just to give another perspective… Do you really need to save the  
sum? Could it be calculated whenever you need it? If you save the sum  
you need to be confident that nothing will cause your data to get out  
of sync, such as some update to ‘test’ that does not update the sum.  
If you decide you do want to save the sum, could the database  
management system handle it for you? E.g. in PostgreSQL you can define  
triggers - in this case a trigger could update the sum for you no  
matter how ‘test’ and ‘test2’ are updated. You’d then have much  
greater faith in the reliability of each sum.

---

<div class="post-metadata">

**Author:** ![herman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/eada6e/32.png) [@herman](https://www.ruby-forum.com/u/herman)\
**Post date:** [January 16, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/saving-results-of-sum-method-into-another-column/157901/6 "2009-01-16T21:31:25Z")

</div>

Works great. Thank you so much!

On Jan 16, 12:41 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
