# Nil being coerced to float

**URL:** <https://www.ruby-forum.com/t/nil-being-coerced-to-float/164453>\
**Category:** Rails\
**Created:** [April 1, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/nil-being-coerced-to-float/164453 "2009-04-01T16:16:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 1, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/nil-being-coerced-to-float/164453/1 "2009-04-01T16:16:13Z")

</div>

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{ |payment| payment.value }

@balance = @total - @payment_total

```

But when no payments had been made it was coming up with an error when  
trying to sum nil, so I did this:

```
 begin
      @payment_total = @payments.sum{ |payment| payment.value }
 rescue
      @payment_total = 0
 end

```

So, when the sum failed it returned @payment\_total as zero. Now, SURELY,  
there’s a better way to do this? I’m sure it’s obvious when you know  
how. Please, enlighten me.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 1, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/nil-being-coerced-to-float/164453/2 "2009-04-01T17:42:27Z")

</div>

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.inject(0) { |sum, p| sum + p.value unless  
p.value.nil? }

Before running either one of these, you can initialize both @total and  
@payment\_total to zero:  
@total, @payment\_total = 0, 0

Hope that helps.

PS: Wonder if you need all of these to be @instance\_variables ?

On Apr 1, 10:16 am, Tom H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 1, 2009, 11:37pm UTC](https://www.ruby-forum.com/t/nil-being-coerced-to-float/164453/3 "2009-04-01T23:37:28Z")

</div>

On Wed, Apr 1, 2009 at 9:16 AM, Tom H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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{ |payment| payment.value }

You can do

@payment\_total = @payments.sum{ |payment| payment.value } if @payments

or

@payment\_total = @payments.sum{ |payment| payment.value } rescue 0

or

@payment\_total = @payments ? @payments.sum{ |payment| payment.value } :  
0

> ```
> end
> 
> ```
> 
> So, when the sum failed it returned @payment\_total as zero. Now, SURELY,  
> there’s a better way to do this? I’m sure it’s obvious when you know  
> how. Please, enlighten me.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 2, 2009, 2:43pm UTC](https://www.ruby-forum.com/t/nil-being-coerced-to-float/164453/4 "2009-04-02T14:43:51Z")

</div>

Thanks Guys, that’s much more Ruby like code!
