Math with table rows

Lets supose I have a User table, and on this table I have height, weight
and
result rows.
In a form I input height and weight values and want to multiply those
two
values and store on the resulta row on database. I was trying something
like
that

  def criar
    @user = User.new(params[:user])
    @result = @user.height * @user.weight
    @user.result = @result

But its not working, what am i doing wrong?

You should be doing this in the model, not controller.
If you want to do this everytime the user record is saved, then define a
before_save callback in your model and add this in the method

def udpate_result
self.result = self.height*self.weight
end

Note: You will have to convert height and weight to integer/float if you
have not defined those columns as just string in the database.

Chirag
http://sumeruonrails.com

2011/8/18 Cssio G. [email protected]

2011/8/18 Cssio G. [email protected]:

In a form I input height and weight values and want to multiply those two
values and store on the resulta row on database. I was trying something like
that
def criar
@user = User.new(params[:user])
@result = @user.height * @user.weight
@user.result = @result
But its not working, what am i doing wrong?

What does “not working” mean?


Hassan S. ------------------------ [email protected]

twitter: @hassan