# Rails Forms...Calculations and things

**URL:** <https://www.ruby-forum.com/t/rails-forms-calculations-and-things/192588>\
**Category:** Rails\
**Created:** [August 2, 2010, 6:24pm UTC](https://www.ruby-forum.com/t/rails-forms-calculations-and-things/192588 "2010-08-02T18:24:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![crashbangboom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9e8a1a/32.png) [@crashbangboom](https://www.ruby-forum.com/u/crashbangboom)\
**Post date:** [August 2, 2010, 6:24pm UTC](https://www.ruby-forum.com/t/rails-forms-calculations-and-things/192588/1 "2010-08-02T18:24:14Z")

</div>

I have a partial form for tank tickets that requires end user  
input…It is designed to minimize said input to reduce the  
possibility for error…There are several other fields where  
calculation results need to go before being sent to the database…!

Questions:

1. Is the ‘Create’ function the place to trigger the  
calcs…?..How…?
2. Where is the best practice location for the calcs…?..(model,  
helper…?..)
3. How do I get the results into the database so they can be  
displayed later on…?

Example:  
User input fields…

1. A drop-down to select a tank number
2. Drop-downs to select a date
3. A Text field to input a ticket number
4. A drop-down to select a product type
5. A Text field to input an API gravity
6. A Text field to input a tank temperature
7. A Text field to input the tank gauge

The remaining five fields are calculated:  
8) Gross Volume (each tank in the database has an ‘increment table’  
used to determine the gross volume using the ‘gauge’)  
9) Volume Correction Factor (calculated from the API gravity and the  
tank temperature)  
10) Roof Correction Factor (this is calculated using data from the  
‘tanks’ table)  
11) Net Volume (this is the result of multiplying the Gross Volume by  
the Volume Correction Factor then adding the Roof Corr)  
12) Ticket Volume (this is determined by comparing this ticket ‘Net  
Volume’ to the last tickets ‘Net Volume’ for the same tank)

I have all the calculations running in rails and they are returning  
the correct results…

So far, the only way I can get all this to work is to have the look-up  
methods in the ‘public\_helper’ and the raw calcs in the  
‘application\_helper’…

Here’s an example of how I’m getting the ‘gross volume’:  
def get\_gross\_vol( gauge )  
@upperinc = Tank202increment.find(:first, :conditions =\> ["inc

> = ?", gauge])  
> @lowerinc = Tank202increment.find(:first, :conditions =\> [“inc \< ?”,  
> gauge], :order =\> ‘inc DESC’)  
> gross\_vol = ((gauge - @lowerinc.inc) \* 100 \* @upperinc.multiplier) +  
> @lowerinc.volume  
> gross\_vol = round(gross\_vol, 2)  
> return gross\_vol  
> end

Here’s a calculation example:

# Calculate Density(kg/m3)

# Inputs:

# Observed API gravity

def get\_density( api )  
density = (141.5 / (131.5 + api.to\_f) \* 999.012)  
density = round(density, 3)  
return density  
end

Thanks in advance for any light shed on this…I am new to Rails…am  
luvin’ it and am trying to get it right…especially with regard to  
‘Best Practices’/‘Standardization’…!
