Ruby 1.9.3-p448 と Rails 3.2.13 を使用しています。float 形式の検証を使用したこの単純なモデルがあります。
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 の値を入力するのはなぜですか? 検証エラーが発生することを期待していました。