I have the code which creates the following made up of an array of 3 TreatLists each with a number of Treatlistitems (I've broken it up to make it easier to read).
?> @breakdown => [ #<Treatlist:0x23b98ac @items=[ #<Treatlistitem:0x2389ef4 @longname="Eye Brow Shape", @spend=17.0, @prodtreat="T", @numsold=2, @unitcost=10.0>]>,
#<Treatlist:0x235df34 @items=[#<Treatlistitem:0x235b16c @longname="Back Massage", @spend=10.0, @prodtreat="T", @numsold=1, @unitcost=10.0>, #<Treatlistitem:0x23597cc @longname="Hopi Ear Candles", @spend=10.0, @prodtreat="T", @numsold=1, @unitcost=10.0>, #<Treatlistitem:0x23582dc @longname="Back Massage (Essential Oils)", @spend=10.0, @prodtreat="T", @numsold=1, @unitcost=10.0>]>,
#<Treatlist:0x2357a1c @items=[ #<Treatlistitem:0x2351b30 @longname="Back Massage", @spend=10.0, @prodtreat="T", @numsold=1, @unitcost=10.0>, #<Treatlistitem:0x234f9fc @longname="Hopi Ear Candles", @spend=10.0, @prodtreat="T", @numsold=1, @unitcost=10.0>, #<Treatlistitem:0x234cf90 @longname="Eye Brow Shape", @spend=10.0, @prodtreat="T", @numsold=1, @unitcost=10.0>, #<Treatlistitem:0x2349db8 @longname="Full Leg Wax DSS", @spend=18.0, @prodtreat="T", @numsold=2, @unitcost=10.0>, #<Treatlistitem:0x23476bc @longname="Bridal Make Up", @spend=10.0, @prodtreat="T", @numsold=1, @unitcost=10.0>, #<Treatlistitem:0x2342fcc @longname="Collin Face Powder", @spend=10.0, @prodtreat="P", @numsold=1, @unitcost=10.0>]>]
I'm trying to add together the 'spend' figures from each but only if prodtreat = T or P. When I try the following:
@breakdown[1].items.sum{|item| item.spend if item.prodtreat == "T"}
=> 30.0
it works!!!
But when I try it with P ...
@breakdown[1].items.sum{|item| item.spend if item.prodtreat == "P"}
I get the following:
NoMethodError: You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.+ from /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/ active_support/core_ext/enumerable.rb:63:in `sum' from (irb):65:in `inject' from /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/ active_support/core_ext/enumerable.rb:63:in `each' from /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/ active_support/core_ext/enumerable.rb:63:in `inject' from /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/ active_support/core_ext/enumerable.rb:63:in `sum' from /Library/Ruby/Gems/1.8/gems/activesupport-2.2.2/lib/ active_support/core_ext/enumerable.rb:61:in `sum' from (irb):65
I really have no idea why this is not working or not returning a zero value?! Any ideas please?
Cheers
Darren