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.

Saludos,
Juan Roberto Morales
Gerente General
Digital Vision Studios

You should initialize @outstanding first before trying to add something
to it. So for example first set @outstanding to zero and then do
@outstanding += @bill.amount

Kind regards,

Nick

http://www.railshostinginfo.com
Compare Rails hosting companies

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.

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.

On 2/24/07, Nick S. [email protected] wrote:

http://www.railshostinginfo.com
Compare Rails hosting companies


Posted via http://www.ruby-forum.com/.


Saludos,
Juan Roberto Morales
Gerente General
Digital Vision Studios

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs