On Wed, Apr 1, 2009 at 9:16 AM, Tom Harvey
<[email protected]> 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


>   �...@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.



-- 
Greg Donald
http://destiney.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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to