# How can i subtraction variables from a form?

**URL:** <https://www.ruby-forum.com/t/how-can-i-subtraction-variables-from-a-form/180348>\
**Category:** Rails\
**Created:** [December 18, 2009, 4:58am UTC](https://www.ruby-forum.com/t/how-can-i-subtraction-variables-from-a-form/180348 "2009-12-18T04:58:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![longhorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c5a1d2/32.png) [@longhorn](https://www.ruby-forum.com/u/longhorn)\
**Post date:** [December 18, 2009, 4:58am UTC](https://www.ruby-forum.com/t/how-can-i-subtraction-variables-from-a-form/180348/1 "2009-12-18T04:58:18Z")

</div>

Hi, i will try to explain me very well, because i dont speak english.  
Well im trying to do this: i have 3 fields in my table: charge, payment,  
balance but the third field balance is a the result of a subtraction,  
but i dont know how to do this.

Because i have a form and after take the data from the form, i insert in  
the table, but before do this i have made this subtraction. And i have  
no idea where can i do this subtraction or how: balance= charge -  
payment, and then when i have a result from this make one insert into a  
table in mysql. But i telling you i have no idea how do this??

My fields are this:

**Cargo:**

**Abono:**

Hope ive made a good explication.

And thank by your help!

---

<div class="post-metadata">

**Author:** ![longhorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c5a1d2/32.png) [@longhorn](https://www.ruby-forum.com/u/longhorn)\
**Post date:** [December 18, 2009, 5:08am UTC](https://www.ruby-forum.com/t/how-can-i-subtraction-variables-from-a-form/180348/2 "2009-12-18T05:08:57Z")

</div>

Carlos Mena wrote:

> Hi, i will try to explain me very well, because i dont speak english.  
> Well im trying to do this: i have 3 fields in my table: charge, payment,  
> balance but the third field balance is a the result of a subtraction,  
> but i dont know how to do this.
> 
> Because i have a form and after take the data from the form, i insert in  
> the table, but before do this i have made this subtraction. And i have  
> no idea where can i do this subtraction or how: balance= charge -  
> payment, and then when i have a result from this make one insert into a  
> table in mysql. But i telling you i have no idea how do this??
> 
> My fields are this:
> 
> **Cargo:**
> 
> **Abono:**
> 
> Hope ive made a good explication.
> 
> And thank by your help!

Hi,  
As per your post. you have to take two fields charge and payment in  
form. during submit you have to use callback method before\_save in model  
and in that you have to set balance fields as self.balance = self.charge

- self.payment. so, before saving record in table it set the balance  
field as a difference of charge & payment.  
Hope, this will help you.

Thanks,  
Priyanka P.

---

<div class="post-metadata">

**Author:** ![longhorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c5a1d2/32.png) [@longhorn](https://www.ruby-forum.com/u/longhorn)\
**Post date:** [December 18, 2009, 6:11am UTC](https://www.ruby-forum.com/t/how-can-i-subtraction-variables-from-a-form/180348/3 "2009-12-18T06:11:55Z")

</div>

I want to thank you for your help, but let me show you how i do this  
without calculate any field:

HTML:

Controller:  
def new  
@fichas = Ficha.new  
end

def create

@fichas = Ficha.new(@params[‘ficha’])  
if @fichas.save  
flash[:notice] = ‘Client was successfully created.’  
redirect\_to :controller =\>“paciente”, :action =\> “list”  
else  
render\_action ‘new’  
end  
end

But you tell me, that i have to do this:

def create  
self.balance = self.charge - self.payment  
@fichas = Ficha.new(@params[‘ficha’])  
if @fichas.save  
flash[:notice] = ‘Client was successfully created.’  
redirect\_to :controller =\>“paciente”, :action =\> “list”  
else  
render\_action ‘new’  
end  
end

That is correct???

---

<div class="post-metadata">

**Author:** ![longhorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c5a1d2/32.png) [@longhorn](https://www.ruby-forum.com/u/longhorn)\
**Post date:** [December 18, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/how-can-i-subtraction-variables-from-a-form/180348/4 "2009-12-18T19:06:29Z")

</div>

Hi, i want to thank you because i resolve my problem and i made it in  
this way, if anyone has the same problem.

HTML:

Controller:  
def new  
@fichas = Ficha.new  
end

def create

@fichas = Ficha.new(@params[‘ficha’])  
if @fichas.save  
flash[:notice] = ‘Client was successfully created.’  
redirect\_to :controller =\>“paciente”, :action =\> “list”  
else  
render\_action ‘new’  
end  
end

AND IN MY MODEL I ADD THIS:

before\_save :resta  
def resta  
self.saldo = self.cargo - self.abono  
end

And that is all, i have to do, thanks again and ruby on rails rules!!

God bless you!
