I have an app that allows you to book meeting rooms, and I am trying to make it so you cannot book a room if the time slot has already been taken.
Here are 2 examples:
- If somebody want’s to book a time slot from 12:00 - 13:00 somebody else should not be able to book that same slot
- If somebody want’s to book a time slot from 12:00 14:00 somebody else should not be able to book 13:00 - 15:00 as this will overlap.
This is my current validation code:
class MeetingValidator < ActiveModel::Validator def validate(record) if record.class.where(to: record.to..record.from).where(from: record.to..record.from) record.errors[:time_slot] << "has already been taken, please pick another" end end end
Any help is much apreciated.