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. -- Posted via http://www.ruby-forum.com/. -- 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.
On 22 September 2010 10:27, Abder-Rahman Ali <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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?You can define a method sales_result on the appropriate model which returns the required value. Colin -- 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.
2010/9/22 Abder-Rahman Ali <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> 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? >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* -- 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.
all of the above and this http://railscasts.com/episodes/14-performing-calculations-on-models On Wed, Sep 22, 2010 at 5:50 AM, Edmond Kachale < edmond.kachale-qPoFbzSWEfuuL8s4vL5bMA@public.gmane.org> wrote:> > 2010/9/22 Abder-Rahman Ali <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > > 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? >> > > 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* > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@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.
On 22 September 2010 12:32, radhames brito <rbritom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> all of the above and thisBecause 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 -- 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.
@michael sorry i forget because i use gmail to post :( -- 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.
Thanks everyone for your replies. Good link @radhames. -- Posted via http://www.ruby-forum.com/. -- 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.
@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. -- Posted via http://www.ruby-forum.com/. -- 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.
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 On Sep 22, 2010, at 8:15 AM, Abder-Rahman Ali wrote:> @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. > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? > > WalterThanks for your reply. I''m looking forward to using this through rails. -- Posted via http://www.ruby-forum.com/. -- 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.
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 -- 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.
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 On Wed, Sep 22, 2010 at 8:27 AM, Abder-Rahman Ali <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> 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. > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks @radhames. That is really important to know about. -- Posted via http://www.ruby-forum.com/. -- 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.