Each time I call validation method on any model, I get the following error:
NoMethodError: undefined method `zero?' for nil:NilClass
It appears that if I remove function sum from my customized Array class the error will go away:
class Array
def sum
inject( nil ) { |sum,x| sum ? sum+x : x }
end
..
Why is this happening?
How can I fix this and still preserve the sum function?
Rails already provides a
sumfunction for arrays (actually on the Enumerable module so that it can be used on other collections)It differs from your function in two respects:
[].sumreturns 0 by default rather than nil[1,2,3].sum {|x| x * 2}returns 12.It’s hard to tell without a backtrace but presumably some code in rails is relying on the first of those 2 behaviours. Remove your definition of the
sumfunction and all should be ok. If you need the sum of the empty list to benilrails’ sum method takes as an optional argument what to return for a empty list