我定义了一个整型字段Mongoid模型 ,我验证numericality这样
# source.rb
class Source
field :code, type: Integer
validates_numericality_of :code, allow_nil: true
allow_nil的目的是要验证的存在&忽略零值的字段。
但在这里,allow_nil完全绕过numericality检查
object = Source.new
object.code = "ABC"
object.valid?
=> true
object
=> #<Source _id: 50d00b2d81ee9eae46000001, _type: nil, code: 0>
在ActiveRecord的,这个工作正常
object = Source.new
object.code = "ABC"
object.valid?
=> false
object
=> #<Source id: nil, code: 0, created_at: nil, updated_at: nil>
object.save
(0.1ms) begin transaction
(0.1ms) rollback transaction
=> false