我正在使用 Ruby 1.9.3-p448 和 Rails 3.2.13。我有一个带有浮点格式验证的简单模型:
class User
include Mongoid::Document
include Mongoid::Timestamps
field :height, type: Float
field :weight, type: Float
validates :height, presence: true, format: { with: /[-+]?[0-9]*\.?[0-9]+/ }
validates :weight, presence: true, format: { with: /[-+]?[0-9]*\.?[0-9]+/ }
end
如果我运行此代码:
test = User.new(height:"hi", weight:"try")
它给了我以下结果:
#<User _id: 51f67b49781018056b000008, created_at: nil, updated_at: nil, height: 0.0,width: 0.0>
如果我放一个字符串,为什么 mongoid 会放一个 0.0 值?我期待一个验证错误。