title: Ruby on Rails: Running custom validation function only when other properties are present
body: <p>I want my custom validator to only run when the properties it uses are present.</p>
  
  <pre><code># these are all dates
  validates :start_time, presence: true
  validates :end_time, presence: true
  validates :deadline_visitor, presence: true
  validates :deadline_host, presence: true
  
  # these validate the above dates
  validate :validate_all_dates
  validate :validate_dates
  </code></pre>
  
  <p>example:</p>
  
  <pre><code>def validate_all_dates
    if self.start_time &gt; self.end_time
      errors.add(:start_time, 'must be before or the same day as end time')
    end
  end
  </code></pre>
  
  <p>fails because <code>self.start_time</code> and <code>self.end_time</code> are not present</p>