房间预订系统查询

时间:2016-09-21 19:17:57

标签: ruby-on-rails

我有以下关联

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

在我的预订模型中,我有以下字段

  

checkin_date,checkout_date

我想找出在给定时间内尚未保留的所有房间。

我写了以下查询,但它没有用。请建议更正或更好的方式。

    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
    )    

2 个答案:

答案 0 :(得分:0)

解决方案

<div class="m1">
  <div>t1</div>
  <div>t2</div>
  <p>M1 Tag closed</p>
</div>

<div class="m2">
  <div>t3</div>
</div>

答案 1 :(得分:0)

也许......

Room.joins('left join reservations on reservations.room_id = rooms.id')
           .where('reservations.checkin_at > ? AND reservations.checkout_at < ?', date_begin, date_end)
           .where('reservations.room_id IS NULL')