Created
August 7, 2014 08:32
-
-
Save barsukov/85c988c7025e88f2e7b2 to your computer and use it in GitHub Desktop.
This is paste of overlaping date usual task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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