简体   繁体   English

在RoR中搜索免费字词

[英]Search free term in RoR

I am looking for a way to write a query that would look for the date of the conference room in which it is free. 我正在寻找一种写查询的方法,该查询将查找免费的会议室的日期。 I have two tables: A table with the names of conference rooms and The booking table consists of two fields: start date [date_from] and end date [date_end] 我有两个表:一个带有会议室名称的表和一个预订表,该表包含两个字段:开始日期[date_from]和结束日期[date_end]

A new client looking for a conference room first types in the search engine the day of renting the room and the ending day. 寻找会议室的新客户首先在会议室租用的日期和结束日期输入搜索引擎。 How to search the registration table to take into account the days booked between those days and display only those conference rooms that are free. 如何搜索注册表以考虑那几天之间的预定日期,并仅显示那些空闲的会议室。

For example, the schedule of the room No. 1 is reserved in the table from March 6, 2018 to March 8, 2018 and room No. 2 from March 4, 2018 to March 7, 2018. I am looking for the room from March 8, 2018 to March 9, 2018 例如,表中保留了1号房间的时间表,从2018年3月6日至2018年3月8日,表2的房间保留了2018年3月4日至2018年3月7日。我正在寻找3月的房间2018年8月8日至2018年3月9日

What ways do you suggest? 您有什么建议?

Assuming model names as ConferenceRoom & ConferenceBooking, hence the corresponding tables will be like conference_rooms & conference_bookings. 假设模型名称为ConferenceRoom和ConferenceBooking,则相应的表将类似于Conference_rooms和Conference_bookings。 Once the date_from and date_end parameters are provided, the query would be look like 一旦提供了date_from和date_end参数,查询将类似于

ConferenceRoom.
  left_joins(:conference_bookings).
  where(
    "conference_bookings.date_from > ? OR 
     conference_bookings.date_end < ?", date_end, date_from
  )

The query fetch results of conference room which has booking end dates less than provided date_from value and conference room which has booking start dates greater than provided date_end value. 查询将获取预订结束日期小于提供的date_from值的会议室的会议室和预订开始日期大于提供的date_end值的会议室的结果。

First get all ids from rooms that are booked during the date range. 首先从日期范围内预订的房间获取所有ID。

ids = Booking.where("date_from <= ? AND date_end >= ?", date_end, date_from).pluck(:room_id)

All other rooms are free: 所有其他房间都是免费的:

free_rooms = Room.where.not(id: ids)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM