# How to store / use a calculated field? - Intermediate Noob -

**URL:** <https://www.ruby-forum.com/t/how-to-store-use-a-calculated-field-intermediate-noob/175761>\
**Category:** Rails\
**Created:** [September 19, 2009, 2:47am UTC](https://www.ruby-forum.com/t/how-to-store-use-a-calculated-field-intermediate-noob/175761 "2009-09-19T02:47:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![internetchris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@internetchris](https://www.ruby-forum.com/u/internetchris)\
**Post date:** [September 19, 2009, 2:47am UTC](https://www.ruby-forum.com/t/how-to-store-use-a-calculated-field-intermediate-noob/175761/1 "2009-09-19T02:47:36Z")

</div>

Hey Everyone

I’m not sure how to ask this properly - that’s probably the reason I’m  
not sure how to implement it. I’m an intermediate noob and I’m not  
sure how to bring this together…

Here’s my scenerio…

people using my system record “readings” every month to determine  
month to month usage. Here’s the calculation…

{ This months reading} - {last months reading} = the usage for this  
month

When adding a new reading record I want the system to look at the two  
readings, calculate the usage, and then insert it possibly using an  
“after\_save” function to store the usage. Here’s the basic idea, but  
I’m not sure how to implement it - any ideas or suggestions would be  
helpful. This seems to be something I would want to handle in my  
model, but then I’m not sure how I call it when I save the record.

def get\_usage(id,lastmonth,reading)

@last\_month=Readings.find(:all, :conditions=[:month =\>  
lastmonth, :id=\>id])  
current\_usage =reading-@last\_month.reading  
return current\_usage  
end

Any help is appreciated.

Thanks

Chris

---

<div class="post-metadata">

**Author:** ![internetchris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@internetchris](https://www.ruby-forum.com/u/internetchris)\
**Post date:** [September 19, 2009, 9:47am UTC](https://www.ruby-forum.com/t/how-to-store-use-a-calculated-field-intermediate-noob/175761/2 "2009-09-19T09:47:50Z")

</div>

2009/9/19 internetchris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> month to month usage. Here’s the calculation…
> 
> def get\_usage(id,lastmonth,reading)
> 
> Â @last\_month=Readings.find(:all, :conditions=[:month =\>  
> lastmonth, :id=\>id])  
> Â current\_usage =reading-@last\_month.reading  
> return current\_usage  
> Â end

A question, is it necessary to store this in the db at all? I am not  
sure from your post but it seems that this could be calculated any  
time it is needed. If that is so then I would suggest, initially at  
least, not storing it at all. Provide a model method, usage or  
whatever is appropriate, that looks up the relevant records and  
returns the value. Then if in the future you decide for efficiency  
reasons that you must save this in the db it is only a matter of  
storing it as appropriate and changing your usage method to return the  
saved value direct from the db. One of the issues with saving it is  
that if you later edit one of the records that it is calculated from  
you must remember to update the saved value.

Colin
