# Need a suggestion for calculated value

**URL:** <https://www.ruby-forum.com/t/need-a-suggestion-for-calculated-value/147903>\
**Category:** Rails\
**Created:** [September 6, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/need-a-suggestion-for-calculated-value/147903 "2008-09-06T20:55:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [September 6, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/need-a-suggestion-for-calculated-value/147903/1 "2008-09-06T20:55:06Z")

</div>

I need a suggestion for a calculated value. I have an order object  
with a total in it. I’d like to recalculate the order total during  
the checkout process (as user selects shipping, etc.) but after the  
checkout is complete, I’d like to store it in the database for  
performance and searching reasons.

Does that make sense? Any suggestions on how to pull this off? I was  
thinking an alias to the database method and checking the status of a  
checkout\_complete boolean.

TIA,  
Sean

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [September 6, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/need-a-suggestion-for-calculated-value/147903/2 "2008-09-06T21:53:40Z")

</div>

On 6 Sep 2008, at 19:54, schof wrote:

> 

You could do something like

def total  
self[:total] || calculate\_total  
end

Which does what it says: if there is a non nil total attribute then it  
will use that, if not it calculates it.

Fred
