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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2011/8/18 Cássio Godinho <cassiopgodinho-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> 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 Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Cássio Godinho <cassiopgodinho-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> 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 received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.