时间的可用性比较,使用Ruby on Rails可用性、时间、Ruby、on

2023-09-11 03:54:19 作者:暖阳

我想有多个对象可用性表,并进行比较。例如,房客将搜索可从1-3PM周一出租单位。

要做到这一点,我想我的一周将分为30分钟的时间段,并给每个时隙的ID。然后将日期范围将搜索对应时隙的ID。

不过,那么我就需要5040条记录,以保持每个时隙的曲目,我不是太激动不已手动保持这种(和Ruby的,我不知道我怎么会去做)。是否有一个更优雅的方法,通过使用日期时间,或者一些Rails插件,会做类似的东西是什么,但我想更容易?

的关键要求是,它必须比较的时间多,隔块。

编辑:这将是一个奖金,如果有可能是一个方法来比较的时候多块,看最好的比赛。举例来说,如果我想租在周一,周三和周五的视频摄像头,一个摄像头可用三个天,但另外一种是仅有两那些日子里,我希望能够在比较和排序的相机依据最佳匹配。

解决方案

您不需要有这样一个复杂的模型。所有你需要的是一个表格,显示的时间块为其租住单位的不可以可用,换句话说,与预订时间为每个出租单元的表。例如:

 创建表预订
(rental_unit_id INT
,start_datetime日期
,end_datetime日期
)
 
fly.io ruby on rails

现在,如果你想要得到的出租单位可用于给定的时间块的整体的名单,说@Arrive到@Depart那么所有你需要做的就是运行这样的查询:

 选择R.rental_unit_id  - (和其他任何你想要的)
从RENTAL_UNITř
其中,不存在(选择B.rental_unit_id
                   从预订乙
                   其中,B.rental_unit_id = R.rental_unit_id
                     和end_datetime> @到达
                     和start_datetime< @Depart)
 

该查询说让我出租单位的名单,其中没有预约重叠感兴趣的搜索期间(@Arrive到@Depart)。请注意,您可以以&lt玩。= @Depart取决于您是否想拥有一个包括或不包括端点为您的预订周期

编辑:处理多可用性块

@OP增加了对多块的可用性的要求。如果你是租用设备多天,然后@Arrive和@Depart只是发生在不同的日期。如果像@ OP的例子中,多天在中间的差距 - 在设备返回,可以出租给别人presumably - 那么你只需要添加额外的,其中不存在条款 - 一个用于所需的可用性每个独立的模块。刚和他们在一起,你会发现出租单位,在所有需要的时间段可供选择。更好或更差的比赛的概念并不真正适用。一个出租单位或者是用或不是。

I want to have an availability table for multiple objects, and compare them. For example, a renter would search for a rental unit which is available from 1-3PM on Monday.

To do this, I thought I would divide the week into 30-minute time slots and give each time slot an ID. Then the date range would search for corresponding timeslot IDs.

However, then I'd need 5040 records to keep track of each timeslot, and I'm not too thrilled about maintaining this manually (and in Ruby I don't know how I would go about it). Is there a more elegant method, by using datetimes, or some Rails plugin that will do something similar to what I want but much more easily?

The critical requirement is that it must compare multiple, separated blocks of time.

EDIT: It would be a bonus if there could be a way to compare multiple blocks of time to see the best match. For instance, if I want to rent a video camera on Monday, Wednesday, and Friday, and one camera is available all three days but another is available only two of those days, I want to be able to compare and rank the cameras on the basis of best match.

解决方案

You don't need to have such a complex model. All you need is a table that shows the blocks of time for which the rental units are not available, in other words, a table with booked times for each rental unit. For example:

create table BOOKING 
( rental_unit_id  int 
, start_datetime  date
, end_datetime    date
)

Now if you want to get a list of rental units that are available for the entirety of a given time block, say @Arrive to @Depart then all you have to do is run a query like this:

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 )

This query says get me a list of rental units where there is no booking which overlaps the search period of interest (from @Arrive to @Depart). Note that you can play with <= @Depart depending on whether you want to have an inclusive or exclusive endpoint for your booking periods.

EDIT: Handling multiple availability blocks

@OP has added a requirement for multiple availability blocks. If you are renting equipment over multiple days then @Arrive and @Depart just happen on different dates. If, as in @OP's example, the multiple days have gaps in the middle - presumably where the equipment is returned and can be rented to someone else - then you just have to add extra where not exists clauses - one for each independent block of desired availability. Just "and" them together and you will find the rental units that are available in all of the desired time blocks. The notion of better or worse matches doesn't really apply. A rental unit is either available or it isn't.