Making a reservation app. How can I check or validate the date for example date_start and date_end has the value of
date_start date_end
26-11-2017 27-11-2017
I will create another reservation which has these starting and end date values but it conflicts with the other values. How can I check for conflicting dates?
date_start date_end
25-11-2017 28-11-2017
Used this to validate the dates in between
Model
validate :no_reservation_overlap
scope :overlapping, ->(period_start, period_end) do
where "((date_start <= ?) and (date_end >= ?))", period_end, period_start
end
private
def no_reservation_overlap
if (Reservation.overlapping(date_start, date_end).any?)
errors.add(:date_end, 'it overlaps another reservation')
end
end
View - choosing the date
<%= f.label :'date_start:' %>
<%= f.date_field :date_start %>
<%= f.label :'date_end:' %>
<%= f.date_field :date_end %>
Sample dates 26 to 27 are already booked/reserved supposedly the it must prevent from inserting 25 to 28 because 26 to 27 are already booked.