Performing field calculations

I have a small Rails application I'm creating.

I have for example a field called "sales_result".

I also have another two fields, namely, "price" and "quantity_sold".

I have the following formula:

sales_result = price * quantity_sold

How can I perform this formula in my rails application? Such that, when
I for example enter the following values:

price = 3.0
quantity_sold = 3

I get this result calculated automatically for "sales_result":

sales_result = 9

Where should I start in the Rails application in performing this
calculation?

Thanks.

You can define a method sales_result on the appropriate model which
returns the required value.

Colin

The Rails’ Way of doing things is to have fat models and thin views. In other ways, most of the code that deals with critical operations like calculations, database manipulations need to be done at a “model” level. Remember that Rails is a Ruby framework, where as Ruby is object oriented. And by being an object-oriented programming language, most of the operations happen within the object itself.

If you want to get the calculations automatically, consider using AJAX.

all of the above and this

http://railscasts.com/episodes/14-performing-calculations-on-models

Because you top-posted, it was "below" not "above" :-/

Please also trim your response to include only the relevant detail, as
we don't all need to receive the same stuff umpteen times.

TIA

@michael sorry i forget because i use gmail to post :frowning:

Thanks everyone for your replies.

Good link @radhames.

@radhames. I see that the link (Screencast) you sent is done mostly on
the console.

What I'm actually asking about is how to define calculation methods that
are used to calculate a specific formula from some fields, and the
result will be the value for another field.

Is there a tutorial or example relating this issue?

Thanks.

Are you looking to make this happen in the browser, say, in JavaScript? Or are you trying to do this within Rails, say using a form submit or an Ajax submit to send the values to the server and get the value back into the page with RJS?

Walter

Walter Davis wrote:

Are you looking to make this happen in the browser, say, in
JavaScript? Or are you trying to do this within Rails, say using a
form submit or an Ajax submit to send the values to the server and get
the value back into the page with RJS?

Walter

Thanks for your reply.

I'm looking forward to using this through rails.

The same way i did it in the image example i gave you .
also watch this

http://railscasts.com/episodes/16-virtual-attributes

the thing is you can add methods to the models and they become available as if they were attributes, they are virtual attributes so oyu can have in your product model.

def tax
price * tax_percent/100
end

and then you can do

@product = Product.find (params[:id])

@product.tax

and @product .tax you return the something like US$15.00

sorrry

and @product .tax you return the something like US$15.00

meanto to say

and with @product.tax you return something like US$15.00

Thanks @radhames. That is really important to know about.