I have following association

```
User:   
  has_many :reservations
  has_many :rooms, through: :reservations
Room:
  has_many :reservations
  has_many :users, through: :reservations
Reservation:
  belongs_to :room
  belongs_to :user

```

In my reservation model I have following fields

> 
> checkin\_date, checkout\_date
> 
> 
> 

I want to find out all rooms that are not already reserved during the given time.

I have written following query but it's not working. Please suggest correction or better way.

```
    Room.joins('LEFT JOIN reservations ON reservations.room_id = rooms.id').where.not(
       'reservations.checkin_at < :requested_end_date AND 
        reservations.checkout_at > :requested_start_date', 
        requested_start_date: date_begin, 
        requested_end_date: date_end
    )    

```