带验证的预订类型系统 - Rails 3.0
尝试“验证”对象日期的唯一性的最佳方法是什么?
--
例如,我有一个房间,一次只能容纳 1 个人。
用户可以提前预订该房间。
目前房间预订时间为3月1日至3月5日。
我想阻止任何人在这些日期再次预订。
因此,如果有人试图预订 2 月 25 日至 3 月 2 日期间的房间,我需要告诉他们不能,因为房间已订满。
--
我正在使用 Rails 3.0
What would be the best approach to trying to "validate" uniqueness of dates for object.
--
So for example, I have a Room, that can only have 1 person in it at a time.
A User can book this Room in advance.
Currently the Room is booked from the 1st of March to the 5th of March.
I want to prevent anyone from booking again on those dates.
So if someone attempts to book a room from the 25th of Feb to the 2nd of March, I need to tell them they cannot, because the room is fully booked.
--
I am using Rails 3.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚编写了一个简单的应用程序来测试这一点。这是我最终在模型验证中使用的内容。
第一个自定义验证检查当前记录的 start_date 是否落在任何记录的 start_date 和 end_date 之间,第二个自定义验证对 end_date 执行相同的操作。您可能希望显示更好的错误消息(例如通过执行类似的查询(使用 .all 而不是 .count,并添加所有记录)与当前记录冲突的记录的开始/结束日期是多少匹配房间的详细信息)
这是我使用的数据库迁移。
I just wrote a simple app to test this out. Here's what I ended up using in the model validation.
The first custom validation checks to see if the current record's start_date falls between the start_date and end_date of any record, and second one does the same for end_date. You might want to show a better error message (like what is the start/end date of the record(s) that is/are clashing with the current one by doing a similar query (using .all instead of .count, and adding all the details of matching rooms).
Here's the db migration I used