:greater_than_or_equal_to in validates_numericality_of only partially working in rails 3.1
Asked Answered
B

4

16

We are using the following to check if stock_qty (an integer or a float. Could be zero but not nil) is greater or equal to zero:

validates_numericality_of :stock_qty, :greater_than_or_equal_to => 0
validates_numericality_of :stock_qty, :less_than_or_equal_to => :in_qty, :if => Proc.new { |part| !part.in_qty.nil? }

:in_qty is a column in part model. This validation should allow positive or 0 for :stock_qty. The problem is that the rspec failed if :stock_qty is assigned zero. I noticed that :less_than_or_equal_to only allowed less_than and did not allow equal_to. Is there a way to validate the >= or <= in rails 3.1? Or what may go wrong with our validation code above. Thanks so much.

Braw answered 17/4, 2012 at 20:6 Comment(1)
The code above seems all right...Braw
U
25

try adding :only_integer => true like so:

validates_numericality_of :stock_qty, :only_integer => true, :greater_than_or_equal_to => 0

EDIT

if this needs to pass when stock_qty is nil or zero you need to change your code to this:

validates_numericality_of :stock_qty, :allow_nil => true, :greater_than_or_equal_to => 0
validates_numericality_of :stock_qty, :allow_nil => true, :less_than_or_equal_to => :in_qty, :if => Proc.new { |part| !part.in_qty.nil? }
Unfriended answered 17/4, 2012 at 20:18 Comment(4)
Just added :stock_qty could be either integer or float. Thanks.Braw
:only_integer => true did not change the rspec result.Braw
Does this test need to pass if stock_qty is nil or zero?Unfriended
stock_qty could be zero, but not nil.Braw
B
12
validates :stock_qty, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }

it works in my 3.1 app, in my case I have price, and when I update or add the product wthout price I got the "it's not a number" error, or something like that, but I can put a 0 in price column and it updates just fine. hope this helps.

:greater_than_or_equal_to – Specifies the value must be greater than or equal to the supplied value. The default error message for this option is “must be greater than or equal to %{count}”.

http://guides.rubyonrails.org/active_record_validations_callbacks.html

Blackandblue answered 17/4, 2012 at 21:40 Comment(2)
Tried exactly the same using validates, rspec still failed with stock_qty == 0. Thanks.Braw
However in rails console, it is OK to save a record with 0 stock_qty. It seems that rspec failed but actual model passed. Strange!Braw
H
1

Also you could believe that there were 0 while there were nil. nil won't pass this checking.

Hoagy answered 17/4, 2012 at 20:32 Comment(2)
Did you mean zero and nil are treated the same in the validation?Braw
No, quite the opposite: you could write something like Model1.new.valid? and got false while stock_qty was nil. That's the only explanation that comes in mind (because I hope you didn't make another validation for you model that fails an you forgot about it).Hoagy
C
0

For Rails > 6.1.0

class User < ApplicationRecord
 validates :age, numericality: { greater_than_or_equal_to: 18, less_than_or_equal_to: 65 }
end

I find the answer in this link

I hope it was easy and clear

Clinquant answered 31/1, 2022 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.