4

I am using Ruby 1.9.3-p448 and Rails 3.2.13. I have this simple model with a float format validation:

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

If i run this code:

test = User.new(height:"hi", weight:"try")

It gives me the following result:

#<User _id: 51f67b49781018056b000008, created_at: nil, updated_at: nil, height: 0.0,width: 0.0> 

Why does mongoid put a 0.0 value if I put a string? I was expecting a validation error.

3
  • What error do you expect? Commented Jul 29, 2013 at 14:41
  • i am expecting a format validation error Commented Jul 29, 2013 at 14:59
  • also when i run User.validators on console it prints [#<Mongoid::Validations::PresenceValidator:0xa7e51d0 @attributes=[:height], @options={}>, # <Mongoid::Validations::PresenceValidator:0xa7e38d0 @attributes=[:weight], @options={}>, #<Mongoid::Validations::PresenceValidator:0xa907824 @attributes=[:chest], @options={}>] Commented Jul 29, 2013 at 15:17

2 Answers 2

3

You get no validation error because the method new doesn't trigger the validation, to see it you should execute:

User.create!(height:"hi", weight:"try")
# .../mongoid-3.1.0/lib/mongoid/persistence.rb:335:in `fail_validate!': (Mongoid::Errors::Validations)
# Problem:
#   Validation of User failed.
# Summary:
#   The following errors were found: Height is invalid, Weight is invalid
# ...

Given that, the fields height and weight are filled with 0.0 because the Strings are converted to Float using the to_f method, that behaves like this:

'foo'.to_f
# => 0.0

Moreover, it is useless to validate a floating point field using a regular expression because the conversion to Float is performed before the validation, so the validation always fails because of the behaviour of =~:

1.2 =~ /any_regexp/
# => nil


Update To validates that, given as string, the field is a valid number you can use the numericality option:

class User
  # ...
  validates :height, presence: true, numericality: true
  # ...
end

User.create!(height: '0.0')
# => #<User ... >

User.create!(height: 'foo')
# Problem:
#  Validation of User failed.
# ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thats exactly my problem thank you! So i must check the format in client side or cani do it in server side by another way?
-1

MongoDB stores data in a binary format called BSON which supports these numeric data types:

  • int32 - 4 bytes (32-bit signed integer)
  • int64 - 8 bytes (64-bit signed integer)
  • double - 8 bytes (64-bit IEEE 754 floating point)

There's no exact value fixed-point equivalent to mySQL's decimal type in MongoDB, but you can store 64-bit floating point numbers in Mongo as a double.

It's worth mentioning that the MongoDB shell - being a JavaScript shell - doesn't recognise the distinction between integer and floating-point values, it treats all numbers the same because JavaScript represents all numbers as 64-bit floating point, regardless of their underlying BSON type.

Most MongoDB language drivers, however, make the distinction between integer and floating point types.

From -> Does MongoDB support floating point types?

2 Comments

i am using mongoid and i cant specity Double as data type i have only Flaot two.mongoid.org/docs/documents/fields.html
Downvoted, because it's an exact copy of this answer. If the answer should have been the same then just link to the other question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.