Hi Friends,
Thanks for viewing my previous blogs, hope you liked them. As we were previously talking about validations starting with Format validations in Rails, lets continue with that with a new validation helper numericality. As its name represents , this helper is used to check whether an attribute is numeric or not.
We can also set only_integer to true if want to allow only integer values.
Example:
class Blogger < ActiveRecord::Base
validates :views, numericality: true
validates :blog_counts, numericality: { only_integer: true }
end
Rails also allows the following helpers that can be used with numericality.
- :greater_than - This is used when we want the attribute value is greater than specified value. Default error message is "must be greater than %{count}
- :greater_than_or_equal_to - This is used when we want the attribute value to be greater than or equal to the specified value. Default error message is "must be greater than or equal to %{count}
- :equal_to -This is used when we want the attribute value to be equal to the specified value. Default error message is "must be equal to %{count}"
- :less_than - This is used when we want the attribute value to be less than the specified value. Default error message is "must be less than %{count}"
- :less_than_or_equal_to - This is used when we want the attribute value to be less than or equal to the specified value. Default error message is "must be less than or equal to %{count}"
- :odd - We will set this field to true, if we want the attribute value to be only odd. Default error message is "must be odd"
- :even - We will set this field to true, if we want the attribute value to be only even. Default error message is "must be even"
Hope you liked this blog too. Read these also for other validations:
0 Comment(s)