Forum: Ruby on Rails Newbie Question

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
0564099250fb8b68fcce19f2ebe6afac?d=identicon&s=25 Juan Roberto Morales (Guest)
on 2007-02-24 10:12
(Received via mailing list)
Hey guys, I have a pretty much noob question here.

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

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

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
would be great.
Juan Roberto Morales
Gerente General
Digital Vision Studios
91308e9bc88cb069fd1bcf88e910d042?d=identicon&s=25 Nick Snels (nicksnels)
on 2007-02-24 11:30
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,

Compare Rails hosting companies
0564099250fb8b68fcce19f2ebe6afac?d=identicon&s=25 Juan Roberto Morales (Guest)
on 2007-02-25 08:06
(Received via mailing list)

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

and I get an error.

On 2/24/07, Nick Snels <> wrote:
> Compare Rails hosting companies
> --
> Posted via
> >

Juan Roberto Morales
Gerente General
Digital Vision Studios
2017657725dd1bce83dc8a1e2e991d04?d=identicon&s=25 Luke Ivers (Guest)
on 2007-02-26 20:43
(Received via mailing list)
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
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
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
values elsewhere in your application.
This topic is locked and can not be replied to.