Newbie Question

Hey guys, I have a pretty much noob question here.

I’m having problems adding values that come from a database, for example if I have several orders in the database, and they have their amounts I can add them using something like

@outstanding = @bill.find_all_by_status('1').sum {|bill| bill.amount}

and it works properly.

However, there are some instances where I would just like to add certain values however everytime I want to do something like this

@outstanding += @bill.amount

It doesn’t work properly. It gives me a nil + error.

I’m sure you’ve all gone through this before. If someone could help me out, would be great.

Nick:

Thanks for the reply.

The problem I’m getting is that say I initialize it to 0 or 0.0 since it’s a float. I’m still getting the same issue.

It’s weird, could you or anyone else show me a working sample of this?

I’ve tried doing this

@outstanding = 0
for item in @items
if item.status == 1
@outstanding += item.amount
end
end

and I get an error.

And you’re sure that item.amount has been initialized?
I would suggest using the script/console command to test this out… script/console opens up an IRB window that will allow you access to all of your models… you can do a find statement just like you would in your actual site

outstanding = 0
Bill.find_by_status_id(1).each do |bill|
puts outstanding, bill.amount
outstanding += bill.amount
end
And see what the values are when it errors (see if it thinks that outstanding is nil, or bill.amount)
If that works perfectly, then you are somehow un-initializing one of these values elsewhere in your application.