I have a Rails app with these 3 models - RentingUnit, Tenant & Booking.
A Tenant can Book a RentingUnit by filling up a form for a new booking with these fields - renting_unit_id, tenant_id, start_date, end_date.
start_date & end_date together form the duration the renting_unit is booked for.
With that, I want to make sure that a renting_unit can not be booked for a duration that overlaps with any duration it's already booked for. (I'm using PostgreSQL database if that matters.)
I came across related answers with Model level validations but I want to enforce uniqueness at the database level too, to account for a possible race condition.
How can I go about implementing it?