带验证的预订类型系统 - Rails 3.0

发布于 2024-10-18 00:43:09 字数 246 浏览 6 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅哥哥的热头脑 2024-10-25 00:43:09

我刚刚编写了一个简单的应用程序来测试这一点。这是我最终在模型验证中使用的内容。

class Booking < ActiveRecord::Base
  validate :uniqueness_of_date_range

  private
  def uniqueness_of_date_range
    errors.add(:start_date, "is not available") unless Room.where("? >= start_date AND ? <= end_date",
                                                            start_date, start_date).count == 0
    errors.add(:end_date, "is not available") unless Room.where("? >= start_date AND ? <= end_date",
                                                            end_date, end_date).count == 0
  end
end

第一个自定义验证检查当前记录的 start_date 是否落在任何记录的 start_date 和 end_date 之间,第二个自定义验证对 end_date 执行相同的操作。您可能希望显示更好的错误消息(例如通过执行类似的查询(使用 .all 而不是 .count,并添加所有记录)与当前记录冲突的记录的开始/结束日期是多少匹配房间的详细信息)

这是我使用的数据库迁移。

class CreateBookings < ActiveRecord::Migration
  def self.up
    create_table :bookings do |t|
      t.date :start_date
      t.date :end_date
      t.string :title

      t.timestamps
    end
  end

  def self.down
    drop_table :bookings
  end
end

I just wrote a simple app to test this out. Here's what I ended up using in the model validation.

class Booking < ActiveRecord::Base
  validate :uniqueness_of_date_range

  private
  def uniqueness_of_date_range
    errors.add(:start_date, "is not available") unless Room.where("? >= start_date AND ? <= end_date",
                                                            start_date, start_date).count == 0
    errors.add(:end_date, "is not available") unless Room.where("? >= start_date AND ? <= end_date",
                                                            end_date, end_date).count == 0
  end
end

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

class CreateBookings < ActiveRecord::Migration
  def self.up
    create_table :bookings do |t|
      t.date :start_date
      t.date :end_date
      t.string :title

      t.timestamps
    end
  end

  def self.down
    drop_table :bookings
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文