Thanks for the replies guys. So, what would be a more sensible way to
write that line?
Thanks in advance.
I would say something like:
errors.add(:price, "should be positive") if !price.nil? && price <= 0.0
Or, to be a bit more verbose:
if !price.nil?
if price <= 0.0
errors.add(:price, "should be positive")
end
end
I guess that's not a huge improvement. Basically, one way or another,
you have to check whether price is nil first, and then, if it isn't,
check whether it's less than or equal to 0, and that's what makes the
line a bit complicated. Hope this helps!
Why not...
errors.add(:price, "should be positive") if price.to_f < 0