James B. Byrne
2006-Jan-31 20:16 UTC
[Rails] asigning values to atributies when source is nil
I want to use something similar to the following: @total_price += { product.price <> nil ? product.price : 0.0 } This fails in rails with a "nil can''t be coerced into Float" error. What is the (most) correct way to do this? On a related issue, what is the significance of the value 40 following "cart.rb" in the trace snippet below? The entire class file is much less than 40 lines long so is this a byte offset? If so, how does one use it to locate the error in the source?> #{RAILS_ROOT}/app/models/cart.rb:40:in `add_product''Regards, Jim -- *** e-mail is not a secure channel *** mailto:byrnejb.<token>@harte-lyne.ca James B. Byrne Harte & Lyne Limited vox: +1 905 561 1241 9 Brockley Drive fax: +1 905 561 0757 Hamilton, Ontario <token> = hal Canada L8E 3C3
Mark Reginald James
2006-Jan-31 22:43 UTC
[Rails] Re: asigning values to atributies when source is nil
James B. Byrne wrote:> I want to use something similar to the following: > > @total_price += { product.price <> nil ? product.price : 0.0 } > > This fails in rails with a "nil can''t be coerced into Float" error. > What is the (most) correct way to do this?@total_price += product.price.to_f> On a related issue, what is the significance of the value 40 > following "cart.rb" in the trace snippet below? The entire class > file is much less than 40 lines long so is this a byte offset? If > so, how does one use it to locate the error in the source? > >>#{RAILS_ROOT}/app/models/cart.rb:40:in `add_product''"@total_price += product.price" is line 40 of http://media.pragprog.com/titles/rails/code/depot_final/app/models/cart.rb -- We develop, watch us RoR, in numbers too big to ignore.
James B. Byrne
2006-Feb-09 16:20 UTC
[Rails] Re: asigning values to atributies when source is nil
Mark Reginald James wrote:>> What is the (most) correct way to do this? > > @total_price += product.price.to_f > >Thank you very much. I missed your original reply in the mailing list digest which accounts for my delay in responding. Regards, Jim -- Posted via http://www.ruby-forum.com/.