Reputation:
I am trying to create a reservation/booking appointment, the user select two dates the starting date and the end date, how can I validate the date from the starting and the end date and also validate the dates in between those two dates preventing the user from choosing again the starting, end date and the dates in between.
Database
date_start | date_end
14/11/2017 14/18/2017
View:
Date field for the dates
<%= f.label :'date_start:' %>
<%= f.date_field :date_start %>
<%= f.label :'date_end:' %>
<%= f.date_field :date_end %>
Model
class Reservation < ApplicationRecord
belongs_to: user
validates :date_start, uniqueness: true
validates :date_end, uniqueness: true
end
I used validates :date_start, uniqueness: true
and validates :date_end, uniqueness: true
but it only checks for the date start and the date_end but it does not check the dates in between them.
Upvotes: 1
Views: 1376
Reputation: 5037
You're going to have to have a separate method to hold the complex validation code. Use validate :method_name
(without an 's') to call it.
The method should add error(s) to the object if it finds a problem.
Something like this:
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
Upvotes: 1
Reputation: 651
validate :check_overlapping
scope :overlaps, ->(date_start, date_end) do
where "((date_start <= ?) and (date_end >= ?))", date_end, date_start
end
def check_overlapping
errors.add(:date_start, 'your error messages') if overlaps(date_start, date_end).any?
end
end
Upvotes: 0