ฉันใช้ 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 ถ้าฉันใส่สตริง ฉันคาดว่าจะมีข้อผิดพลาดในการตรวจสอบความถูกต้อง