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,
Use the gem validates_overlap.
gem 'validates_overlap'
For example, if you have a model called Meeting
with a start_date
and end_date
fields of type date
, you can easily validate that they don't overlap.
class Meeting < ActiveRecord::Base
validates :start_date, :end_date, overlap: true
end
Another more realistic example, say a Meeting
belongs_to a User
, you can scope it out, so it only validates meetings for a particular user.
class Meeting < ActiveRecord::Base
belongs_to User
validates :start_date, :end_date, overlap: { scope: 'user_id',
message_content: 'overlaps with Users other meetings.' }
end