Ramon Miguel M. Tayag
2007-Oct-07 12:19 UTC
Best approach - letting users put their own temp. fields to see diff
Hey everyone! I need some input on the best approach. I have a model Property that has some fields (price, down payment rate), then methods in property.rb that compute derived information (down payment amount) based on what the users input. Sounds simple enough. I. PROBLEM Here''s where I need help: I want to make a "property calculator" of sorts that takes a property, and allows users to input their numbers without overwriting the current property to see how the outcome (value) of the property/investment changes based on those numbers. For example... the property saved in the database has a price of 100,000.00 and down payment rate of 10%. Derived information from that: down payment is 10,000.00. In the property calculator, I want to input 20% to see how the derived information will change, but I don''t want to save this into the property. II. THE WAY I THOUGHT OF DOING IT I know how to do it, but it''s very tedious, and I know that once something becomes tedious in Rails and I''m probably approaching things wrong. I was thinking of putting options in each of the methods in the Property class. For example, def down_payment(options={}) options.stringify_keys! dpr = options[''down_payment_rate''].to_f || down_payment_rate price * dpr end This sounds nice and simple enough at the start but I have many of these derived methods and putting this options thing in all of them would be a hassle. How do you suggest I approach this? Thank you -- Ramon Tayag --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Michael Wang
2007-Oct-08 06:09 UTC
Re: Best approach - letting users put their own temp. fields to see diff
Ramon Miguel M. Tayag wrote:> Hey everyone! > > I need some input on the best approach. I have a model Property that > has some fields (price, down payment rate), then methods in > property.rb that compute derived information (down payment amount) > based on what the users input. Sounds simple enough. > > I. PROBLEM > Here''s where I need help: I want to make a "property calculator" of > sorts that takes a property, and allows users to input their numbers > without overwriting the current property to see how the outcome > (value) of the property/investment changes based on those numbers. > > For example... the property saved in the database has a price of > 100,000.00 and down payment rate of 10%. Derived information from > that: down payment is 10,000.00. In the property calculator, I want to > input 20% to see how the derived information will change, but I don''t > want to save this into the property. > > II. THE WAY I THOUGHT OF DOING IT > I know how to do it, but it''s very tedious, and I know that once > something becomes tedious in Rails and I''m probably approaching things > wrong. > > I was thinking of putting options in each of the methods in the > Property class. For example, > > def down_payment(options={}) > options.stringify_keys! > dpr = options[''down_payment_rate''].to_f || down_payment_rate > price * dpr > end > > This sounds nice and simple enough at the start but I have many of > these derived methods and putting this options thing in all of them > would be a hassle. > > How do you suggest I approach this? > > Thank youWhy would anything be saved back to the database in the first place? Or are you doing the math in your app rather than doing it in JavaScript? -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Ramon Miguel M. Tayag
2007-Oct-08 11:17 UTC
Re: Best approach - letting users put their own temp. fields to see diff
The math will be done in the app.. :) I don''t mean to save the fields into the database if it''s just the calculator I''m accessing. On 10/8/07, Michael Wang <rails-user-JtyympAsP2K7zZZRDBGcUA@public.gmane.org> wrote:> > Why would anything be saved back to the database in the first place? Or > are you doing the math in your app rather than doing it in JavaScript? > > > -- > Michael Wang-- Ramon Tayag --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Ashley Thomas
2007-Oct-08 12:52 UTC
Re: Best approach - letting users put their own temp. fields
Just use the original Property to put the initial view together, then when changes come in make a new Property from the params but don''t save it, just use it to put the requested view together. def update if params[:commit] == ''Preview Changes'' @property = Property.new(params[:property]) render :action => ''edit'' else # update the saved property @property = Property.find(params[:id]) if @property.update_attributes(params[:property]) flash[:notice] = ''Property was successfully updated.'' redirect_to :action => ''show'', :id => @property else render :action => ''edit'' end # if end # if end # update ...or something like that (maybe your edit action is called "calculator" and your update action is called "calculate"). As long as you don''t call save or update_attributes on the property, it will be used in the view but won''t get saved to the database. If you don''t want to give them the option of saving the changes, make it a separate method that just does the first part. -- 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---