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
  • PrintPrint
Share this Page URL
Help

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

A Place on the Calendar

You could test date components individually, but more typically you’ll want to test whether or not the date falls within a given range. Rails makes this easy with the validates_inclusion_of method, already examined previously, and its inverse, validates_exclusion_of:

# Restrict birthday to reasonable values, i.e., not in the future and not
# before 1900
validates_inclusion_of :birthday,
  :in => Date.civil(1900, 1, 1) .. Date.today,
  :message => "must be between January 1st, 1900 and today"

The :in parameter actually takes a list of possible values (an enumerable object, technically), and in this case, the definition creates a list of values between January 1, 1900 (thanks to the Date.civil method) and today’s date (thanks to the Date.today method).


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint