title: Rails validate uniqueness of date ranges
body: <p>I have an application that involves absence records for employees.</p>
  
  <p>I need to ensure that the start and end dates for each record don't overlap.</p>
  
  <p>So for example, if I entered an absence record that started today and ended tomorrow, it shouldn't be possible to enter another inside of that date range in any way. So I couldn't make one that starts the day before today, then ends the day after tomorrow, or any later date.</p>
  
  <p>To put it simply, I need to make the date range unique.</p>
  
  <p>What is the best way to achieve this?</p>
  
  <p>Custom validators in the model class that involve iterating through all records take far too long to complete, and I haven't found any gems that address this problem. I also haven't found any simply way to scope by uniqueness in the model either. I'm stumped :/</p>
  
  <p>Thanks for your time!</p>
  
  <p>Edit:</p>
  
  <pre><code>class Absence &lt; ActiveRecord::Base
  attr_accessible :date, :date_ended, :status, :reason, :form, :user_id, :tempuser, :company_id
  belongs_to :user
  default_scope { where(company_id: Company.current_id) }
  
  validates :date, :date_ended, :status, :reason, :form, :user_id, presence: true 
  validates_numericality_of :user_id, :only_integer =&gt; true, :message =&gt; "can only be whole number."
  end
  </code></pre>