[Rails] Re: Nil being coerced to float

2009-04-02 Thread Tom Harvey
Thanks Guys, that's much more Ruby like code! -- 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-t

[Rails] Re: Nil being coerced to float

2009-04-01 Thread Greg Donald
On Wed, Apr 1, 2009 at 9:16 AM, Tom Harvey wrote: > > I was doing this: > >     @line_items = LineItems.find_all_by_invoice_id(params[:id]) >     @payments = Payments.find_all_by_invoice_id(params[:id]) > >   �...@total = @line_items.sum{ |item| item.cost } >   �...@payment_total = @payments.sum{

[Rails] Re: Nil being coerced to float

2009-04-01 Thread Harold
All you need to do to your original code is add a condition to make sure @payments is not null: @payment_total = @payments.sum{ |payment| payment.value } unless @payments.nil? Or can payment.value be nil for some payments but not others? Inject could help in that case: @payment_totals = @payments