Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

7. Strengthening Models with Validation > A Place on the Calendar - Pg. 114

less_than Tests that the value being validated is less than the value provided in the parameter. less_than_or_equal_to Tests that the value being validated is less than or equal to the value provided in the parameter. odd Tests that the value is an odd number (dividing by 2 yields a remainder of one). only_integer Tests that the value being validated is an integer, with no fractional part. The named parameters have values. For the methods that make comparisons, the value is the argument against which the incoming value will be compared. These can be simple values or method calls, such as :less_than_or_equal_to => Time.now.year . For the boolean tests ( even , odd , only_integer ), the value specifies whether or not the test counts for validation, and the default value for all of them is false . The next two fields, :body_temperature and :price , are also numbers, with relatively simple validations: # Body temperature doesn't have to be a whole number, but we ought to # constrain possible values. We assume our users aren't in cryostasis. validates_numericality_of :body_temperature, :allow_nil => true, :greater_than_or_equal_to => 60, :less_than_or_equal_to => 130, :only_integer => false