这似乎很简单,但我想要求一个数字并确保它不大于或小于预定数量:
validates :age_min, presence: true, numericality: {
greater_than: 0, less_than_or_equal_to: :age_max
}
该测试按预期工作
test 'user should not be valid with age min greater than age max' do
user = FactoryGirl.build(:user, age_min: 30, age_max: 20)
assert !user.valid?
end
但是,当我尝试测试时,这age_min
是必需的:
test 'user should not be valid without age_min' do
user = FactoryGirl.build(:user, age_min: nil, age_max: 20)
assert !user.valid?
end
我明白了ArgumentError: comparison of Float with nil failed
Rails 没有考虑 nil 值似乎很奇怪,或者我错过了什么?看来您应该能够在不编写自定义验证器的情况下使其工作,但也许我弄错了。