Skip to content

Instantly share code, notes, and snippets.

@barsukov
Created August 7, 2014 08:32
Show Gist options
  • Save barsukov/85c988c7025e88f2e7b2 to your computer and use it in GitHub Desktop.
Save barsukov/85c988c7025e88f2e7b2 to your computer and use it in GitHub Desktop.
This is paste of overlaping date usual task
class Interval < ActiveRecord::Base
validates_presence_of :start_date, :end_date
# Check if a given interval overlaps this interval
def overlaps?(other)
(start_date - other.end_date) * (other.start_date - end_date) >= 0
end
# Return a scope for all interval overlapping the given interval, including the given interval itself
named_scope :overlapping, lambda { |interval| {
:conditions => ["(DATEDIFF(start_date, ?) * DATEDIFF(?, end_date)) >= 0", interval.end_date, interval.start_date]
}}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment