Rails validate uniqueness of date ranges

前端 未结 5 955
情深已故
情深已故 2021-02-05 23:33

I have an application that involves absence records for employees.

I need to ensure that the start and end dates for each record don\'t overlap.

So for example,

5条回答
  •  执笔经年
    2021-02-05 23:37

    As a modification to the accepted answer, here's an overlaps scope that will work for DBs that don't understand DATEDIFF

      scope :overlaps, ->(start_date, end_date) do
        where "((start_date <= ?) and (end_date >= ?))", end_date, start_date
      end
    

    This draws on the solution for Determine Whether Two Date Ranges Overlap

提交回复
热议问题