ruby on rails - what is wrong with this callback (deal with NaN) -
i have app dealing lots of calculations.
instead of changing corresponding value(s) 0 raises nan.
before_save :change_nan_to_zero def change_nan_to_zero self.attributes.each_pair |key, value| if value == float::nan value = 0 end end end or, maybe (there is) there improve way deal nan (particularly alter everywhere 0). thx!
ok, got it. issue nil saved nan, calculations (results of not saved db) issued nan. i've ended helper views (containing calculations results):
def zero_or_value(value) if value.is_a?(float) value.nan? ? 0 : value else value end end thanks help! appreciated! @marek, sure reply right (i've used solution partially, sure i'll upvote help, guys!)
your solution doesn't work, because set local variable value 0. also, check if float nan, should utilize nan? method (comparison float::nan won't work). should is:
if value.is_a?(float) && value.nan? write_attribute(key, 0) end or, in shorter version:
write_attribute(key, 0) if value.nan? ruby-on-rails ruby
No comments:
Post a Comment