# \[Newb\] Adding database fields to get a total

**URL:** <https://www.ruby-forum.com/t/newb-adding-database-fields-to-get-a-total/174308>\
**Category:** Rails\
**Created:** [August 24, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/newb-adding-database-fields-to-get-a-total/174308 "2009-08-24T20:51:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KRKRKR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cab0a1/32.png) [@KRKRKR](https://www.ruby-forum.com/u/KRKRKR)\
**Post date:** [August 24, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/newb-adding-database-fields-to-get-a-total/174308/1 "2009-08-24T20:51:39Z")

</div>

Total newb here, as I’ve been working my way threw the tutorial, but  
doing my own thing.  
I’ve created a fairly simple app, that has a bunch of items. Each  
item has 6-9 fields that have a cost associated with them (db field =  
float). Think of each item as a car maintenance log, oil change =  
$50.00, tires = 500, breaks = 230 etc… I want to be able to add up  
all the fields associated with the item for a grand total.

I’ve read a lot and I"m not sure if I’m modifying the model, or coming  
up with something fancy in the controller etc…

Any help with my baby steps is much appreciated.

THx

---

<div class="post-metadata">

**Author:** ![KRKRKR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cab0a1/32.png) [@KRKRKR](https://www.ruby-forum.com/u/KRKRKR)\
**Post date:** [August 24, 2009, 9:16pm UTC](https://www.ruby-forum.com/t/newb-adding-database-fields-to-get-a-total/174308/2 "2009-08-24T21:16:49Z")

</div>

I’d suggest keeping that logic in the model…maybe create a method on  
your model for getting “grand\_total”.

This blog post should give you a better idea of why you’d want this  
kind of logic in the model:

[http://railstips.org/2008/12/30/move-it-to-the-model-and-use-tiny-methods](http://railstips.org/2008/12/30/move-it-to-the-model-and-use-tiny-methods)

---

<div class="post-metadata">

**Author:** ![KRKRKR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cab0a1/32.png) [@KRKRKR](https://www.ruby-forum.com/u/KRKRKR)\
**Post date:** [August 24, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/newb-adding-database-fields-to-get-a-total/174308/3 "2009-08-24T21:22:07Z")

</div>

KR wrote:

> Total newb here, as I’ve been working my way threw the tutorial, but  
> doing my own thing.  
> I’ve created a fairly simple app, that has a bunch of items. Each  
> item has 6-9 fields that have a cost associated with them (db field =  
> float). Think of each item as a car maintenance log, oil change =  
> $50.00, tires = 500, breaks = 230 etc… I want to be able to add up  
> all the fields associated with the item for a grand total.
> 
> I’ve read a lot and I"m not sure if I’m modifying the model, or coming  
> up with something fancy in the controller etc…
> 
> Any help with my baby steps is much appreciated.
> 
> THx

You -could- do it this way:

Table item  
… list of items

Table work\_done  
… list of things done

Table work\_done\_on\_items  
… item\_id and work\_done\_id

And you could then just run a simple query for the sum of all the things  
associated with that item… Or if you really wanted to, put the ‘total’  
in the ‘item’ table and then you’d have to do “on\_change” on the other  
table so it updates the total every time an item is added, removed, or  
changed…

hope this helps.

---

<div class="post-metadata">

**Author:** ![KRKRKR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cab0a1/32.png) [@KRKRKR](https://www.ruby-forum.com/u/KRKRKR)\
**Post date:** [August 24, 2009, 9:25pm UTC](https://www.ruby-forum.com/t/newb-adding-database-fields-to-get-a-total/174308/4 "2009-08-24T21:25:36Z")

</div>

Create a virtual attribute in the model that returns the grand total.

def grand\_total  
field\_1 + field\_2 …  
end

You can then reference grand\_total from your views like any other  
attribute.

---

<div class="post-metadata">

**Author:** ![KRKRKR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cab0a1/32.png) [@KRKRKR](https://www.ruby-forum.com/u/KRKRKR)\
**Post date:** [August 24, 2009, 9:43pm UTC](https://www.ruby-forum.com/t/newb-adding-database-fields-to-get-a-total/174308/5 "2009-08-24T21:43:16Z")

</div>

E. Litwin wrote:

> Create a virtual attribute in the model that returns the grand total.
> 
> def grand\_total  
> field\_1 + field\_2 …  
> end
> 
> You can then reference grand\_total from your views like any other  
> attribute.

I probably like that answer the best so far 🙂

---

<div class="post-metadata">

**Author:** ![KRKRKR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cab0a1/32.png) [@KRKRKR](https://www.ruby-forum.com/u/KRKRKR)\
**Post date:** [August 25, 2009, 12:45am UTC](https://www.ruby-forum.com/t/newb-adding-database-fields-to-get-a-total/174308/6 "2009-08-25T00:45:13Z")

</div>

Sweet thanks it worked. Not so bad after all!

On Aug 24, 12:43 pm, Aldric G. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![KRKRKR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cab0a1/32.png) [@KRKRKR](https://www.ruby-forum.com/u/KRKRKR)\
**Post date:** [August 25, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/newb-adding-database-fields-to-get-a-total/174308/7 "2009-08-25T17:41:36Z")

</div>

Some good replies already, but I’d add that you almost certainly want  
to avoid float fields for money. They are OK for some things, but  
people get twitchy when money doesn’t add up exactly. You’re probably  
better off either using decimal fields or just storing values in  
integer cents.

–Matt J.
