How can i subtraction variables from a form?

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!

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.

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???

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!