title: How to implement custom date validation in Ruby on Rails
body: <p>I am trying to follow the example in Rails Guides to implement a custom validation, but I can't seem to make it work to validate a date.</p>
  
  <p>Here is the migration:</p>
  
  <pre><code>class CreateSetLists &lt; ActiveRecord::Migration
    def change
      create_table :set_lists do |t|
        t.date :show_date
        t.string :show_name
  
        t.timestamps
      end
    end
  </code></pre>
  
  <p>end</p>
  
  <p>Here is the model:</p>
  
  <pre><code>class SetList &lt; ActiveRecord::Base
    validates :show_date, :show_name, presence: true
    validate :must_have_valid_future_date
  
    private
  
    def must_have_valid_future_date
      if show_date.present?
          unless show_date.is_a?(Date)
              errors.add(:show_date, "must be a valid date")
          end
  
          if show_date &lt; Date.today
              errors.add(:show_date, "can't be in the past")
          end
      end
    end
  
  end
  </code></pre>
  
  <p>When I try to test this by submitting an invalid date, the errors hash for my object shows a message as if the date were never set. If I remove the "presence" validation, then when I submit an invalid date the object simply saves with nil for the date.</p>
  
  <p>Is there something special about validating that I have valid date that I am missing?</p>
  
  <p>UPDATE (2015-12-07 11:45am):</p>
  
  <p>I noticed this playing around in the console:</p>
  
  <pre><code>irb(main):044:0&gt; msl=SetList.new
  =&gt; #&lt;SetList id: nil, show_name: nil, show_date: nil, created_at: nil, updated_at: nil&gt;
  irb(main):045:0&gt; msl.show_date="invalid"
  =&gt; "invalid"
  irb(main):046:0&gt; msl.show_date
  =&gt; nil
  </code></pre>
  
  <p>It would appear Rails does not allow me to validate that I have a valid date. It simply throws away invalid dates and sets the field to nil.</p>
  
  <p>Is this some property of the column type for that attribute being of type Date? Is that behavior I can rely on, therefore eliminating the need to validate that I have a valid date (because Rails will automatically throw away an invalid date)?</p>
  
  <p>UPDATE (2015-12-07 8:04pm):</p>
  
  <p>This question seems to imply that I can count on Rails throwing away invalid dates without any validation needed in my model:</p>
  
  <p><a href="https://stackoverflow.com/questions/7314400/rails-3-1-date-validation-is-acts-like-date-enough-or-even-necessary?rq=1">rails 3.1 date validation - is acts_like_date? enough or even necessary?</a></p>