Q:

Rails - Is a date between two dates?

I'm creating a system that needs to make sure planned "Trips" do not over lap. To do this I am trying to validate the start_date and end_date to make sure they are not within the start and end dates for another trip.
class TripdateValidator < ActiveModel::EachValidator

  def validate_each(object, attribute, value)

      # Check value exists as end_date is optional
      if value != nil

          parsed_date = Date.parse(value)

          # is the start date within the range of anything in the db
         trips = Trip.where(['start_date >= ? AND end_date <= ? AND user_id = ?', parsed_date, parsed_date, object.user_id])

        # overlapping other trips, aghhh
        object.errors[attribute] << 'oh crap' if trips
      end
  end
end

The date is coming from a type="date" field which is powered by jQuery UI Datepicker and contains the format 2011-01-01. This gets to rails as 2011-01-30 00:00:00 UTC which I don't fully understand.
If I try to use Date.parse() on this value it gives the error:

TypeError in TripsController#create
$_ value need to be String (nil given)
Rails.root:
  /Users/phil/Sites/travlrapp.com
  Application Trace | Framework Trace |
  Full Trace  
app/models/trip.rb:29:in
validate_each'
  app/controllers/trips_controller.rb:75:in
  create'
  app/controllers/trips_controller.rb:74:in
  `create'  

Whenever I run the query, nothing is returned. Could this be a date format issue, is my logic broken or am I doing something really stupid? Been stuck on this a while and google is no help.
Edit People are focusing on the Date.parse error but that is not the main problem. Where im stuck is that I don't understand how to do date comparisons when everything is in totally different formats.
I have swapped Date.parse() for Chronic.parse() and now I am getting the following SQL queries generated:
 SELECT "trips".* FROM "trips" WHERE (start_date >= '2011-01-26 00:00:00.000000' AND end_date <= '2011-01-26 00:00:00.000000' AND user_id = 19)

This returns nothing. The dates I am testing against are: 
start: 2011-02-17 00:00:00.000000
end: 2011-02-21 00:00:00.000000
Seeing as I think dates are being formatted properly now it seems more like a logic problem. How the heck can I validate overlapping dates >.

A:

Squeel power FTW!!! I'm sure you will love it
$ gem install squeel
model.rb
Model.where{date_colum > 10.years.ago & date_column < DateTime.now}

A:

I'm responding to your edit, mostly, and I think you're not getting any results for two reasons:
First:  I suspect your parser is giving you DateTime objects, while you really just want plain Date objects.  Pass a DateTime into Rails's SQL query generator, and it gives you a 'YYYY-MM-DD hh.mm.ss.uuu...' string, while the database expects just a plain 'YYYY-MM-DD' string for date column comparison; the DB date parser fails, gives back NULL, all comparisons with NULL fail, etc., and you get back an empty result set.
Change parsed_date to parsed_date.to_date and you should be okay on that front.
Second:  Take a look at your SQL, this chunk in particular start_date >= ? AND end_date <= ?.  It looks to me like it'll only detect a collision if the trip you're checking against has a start date that's later than its end date.
So try start_date <= ? AND end_date >= ? or ? BETWEEN start_date AND end_date, which I personally prefer, as it's a bit more readable.
Hope this helps!

A:

For overlap validation you can try one of these gems https://github.com/chrisb87/range_validator or https://github.com/robinbortlik/validates_overlap

