Ruby on rails 使用RubyonRails进行时间可用性比较
我想为多个对象创建一个可用性表,并对它们进行比较。例如,租客会搜索周一下午1-3点可用的租赁单元 要做到这一点,我想我应该将一周划分为30分钟的时间段,并给每个时间段一个ID。然后日期范围将搜索相应的时间段ID 然而,我需要5040条记录来记录每个时隙,我不太喜欢用Ruby手动维护它,我不知道该怎么做。有没有一种更优雅的方法,通过使用datetimes,或者一些Rails插件,可以做一些类似于我想要的但更容易的事情 关键要求是它必须比较多个独立的时间块Ruby on rails 使用RubyonRails进行时间可用性比较,ruby-on-rails,ruby,algorithm,ruby-on-rails-3,database-design,Ruby On Rails,Ruby,Algorithm,Ruby On Rails 3,Database Design,我想为多个对象创建一个可用性表,并对它们进行比较。例如,租客会搜索周一下午1-3点可用的租赁单元 要做到这一点,我想我应该将一周划分为30分钟的时间段,并给每个时间段一个ID。然后日期范围将搜索相应的时间段ID 然而,我需要5040条记录来记录每个时隙,我不太喜欢用Ruby手动维护它,我不知道该怎么做。有没有一种更优雅的方法,通过使用datetimes,或者一些Rails插件,可以做一些类似于我想要的但更容易的事情 关键要求是它必须比较多个独立的时间块 编辑:如果有一种方法可以比较多个时间段以查
编辑:如果有一种方法可以比较多个时间段以查看最佳匹配,那将是一种奖励。例如,如果我想在星期一、星期三和星期五租一台摄像机,其中一台摄像机在三天内都可用,而另一台仅在两天内可用,我希望能够根据最佳匹配情况对摄像机进行比较和排序。我想说,您的时段应为以下型号:
TimeSlot
rental_id : int -> foreign key to your rental table (housing, whatever)
start_time : time
end_time : time
day_of_week : int 1-7
然后在两次之间从可用性进行搜索:让我们称这些可用的\u插槽
(
(wanted_start_time >= TimeSlotTable.start_time && wanted_start_time <= TimeSlotTable.end_time) OR
(wanted_end_time >= TimeSlotTable.start_time && wanted_end_time <= TimeSlotTable.end_time)
)
AND Optionally:
( wanted_day_of_week = TimeSlotTable.day_of_week)
和查询:
( wanted_date = ExceptionTimeSlotTable.date)
最后,您希望租赁的\u id位于可用的\u时隙中,但不在例外情况中
整个过程就像一个事件日历,因此您可以选择一个均匀的日历,并根据自己的目的进行自定义,并调用事件时间段。您不需要有如此复杂的模型。您所需要的只是一个表格,其中显示了租房单位不可用的时间段,换句话说,一个包含每个租房单位预订时间的表格。例如:
create table BOOKING
( rental_unit_id int
, start_datetime date
, end_datetime date
)
现在,如果您想获得整个给定时间段内可用的租赁单元列表,比如说@Arrival to@Deep,那么您所要做的就是运行如下查询:
select R.rental_unit_id -- (and anything else you want)
from RENTAL_UNIT R
where not exists ( select B.rental_unit_id
from BOOKING B
where B.rental_unit_id = R.rental_unit_id
and end_datetime > @Arrive
and start_datetime < @Depart )
这个查询说,给我一个没有预订的出租单元列表,该列表与从@Arrival到@Deep的搜索期重叠。请注意,您可以使用,也许您应该创建模型时间段,该时间段具有多个:租赁单位,这样您可以在数据库中只保存必要的时间段,而且管理相关事件/对象也会更容易。
select R.rental_unit_id -- (and anything else you want)
from RENTAL_UNIT R
where not exists ( select B.rental_unit_id
from BOOKING B
where B.rental_unit_id = R.rental_unit_id
and end_datetime > @Arrive
and start_datetime < @Depart )