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.

On 22 September 2010 10:27, Abder-Rahman A. [email protected]
wrote:

How can I perform this formula in my rails application? Such that, when
calculation?
You can define a method sales_result on the appropriate model which
returns the required value.

Colin

2010/9/22 Abder-Rahman A. [email protected]

How can I perform this formula in my rails application? Such that, when
calculation?

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.


Edmond
Software Developer | Baobab Health Trust (http://www.baobabhealth.org/)
|
Malawi

Cell: +265 999 465 137 | +265 881 234 717

“Many people doubt open source software and probably don’t realize that
there is an alternative… which is just as good…” – Kevin Scannell

all of the above and this

On Wed, Sep 22, 2010 at 5:50 AM, Edmond K. <

On 22 September 2010 12:32, radhames brito [email protected] wrote:

all of the above and this

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

Thanks everyone for your replies.

Good link @radhames.

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

@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 D. 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.

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

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

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

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