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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- railshostinginfo.com Compare Rails hosting companies -- Posted via ruby-forum.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Snels <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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 > -- > railshostinginfo.com > Compare Rails hosting companies > > -- > Posted via ruby-forum.com. > > > >-- Saludos, Juan Roberto Morales Gerente General Digital Vision Studios --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On 2/25/07, Juan Roberto Morales <dvstudios-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 Snels <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > > 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 > > -- > > railshostinginfo.com > > Compare Rails hosting companies > > > > -- > > Posted via ruby-forum.com. > > > > > > Digital Vision Studios > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---