Forum: Ruby on Rails asigning values to atributies when source is nil

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James B. Byrne (Guest)
on 2006-01-31 22:19
(Received via mailing list)
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 J. (Guest)
on 2006-02-01 00:45
(Received via mailing list)
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_...

--
We develop, watch us RoR, in numbers too big to ignore.
James B. Byrne (Guest)
on 2006-02-09 18:20
Mark Reginald J. 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
This topic is locked and can not be replied to.