Validates_Overlap Gem多个范围覆盖彼此

我正在使用Validates_Overlap Gem,可以在这里找到: https : //github.com/robinbortlik/validates_overlap

实质是我有两个房间可以预订。 我希望validation在同一房间已经在SAME房间进行确认预订时介入。 如果预订了其他房间,或者预订了相同的房间但尚未确认,则不应该给我一个错误。

到目前为止,我的代码如下

validates :start_time, :end_time, :overlap => { :exclude_edges => ["starts_at", "ends_at"], :scope => { "bookings.studio_id" => proc {|booking| booking.studio_id}} && { "bookings.is_confirmed" => proc {|booking| booking.is_confirmed == true}} }, on: :update 

这将从我的服务器返回以下内容:

 Booking Exists (0.4ms) SELECT 1 AS one FROM "bookings" WHERE ((bookings.end_time IS NULL OR bookings.end_time >= '2014-10-23 20:00:00.000000') AND (bookings.start_time IS NULL OR bookings.start_time <= '2014-10-24 03:00:00.000000') AND bookings.id != 9 AND bookings.is_confirmed = 't') LIMIT 1 

还有另外两个预订(使用此studio_id)并且没有一个被确认。 是什么赋予了?

以下是所有预订:studio_id => 2

 [#, #, #] 

更新我注意到在范围行中的&&没有注意到studio_id。 我怎样才能让范围注册? 我可以在范围内执行此操作,还是应该创建方法?

我也尝试过更简单的方法

  validates :start_time, :end_time, :overlap => { :exclude_edges => ["starts_at", "ends_at"], :scope => "is_confirmed" && "studio_id" }, on: :update 

这也是一样的 – 只使用后面的“studio_id”

我知道,选项的名称令人困惑,我很抱歉。

我建议你实现你的命名范围:确认并将其传递为:query_option参数。

我想,它应该是这样的:

 class Booking < ActiveRecord::Base scope :confirmed_scope, -> {confirmed: true} validates :start_time, :end_time, :overlap => { :exclude_edges => ["starts_at", "ends_at"], :scope => "studio_id", :query_options => {:confirmed_scope => nil} }, on: :update end 

顺便说一下……如果你使用Rails 4.1,请小心,有一个更改https://github.com/robinbortlik/validates_overlap#rails-41-update

简短说明:您传递的内容为:scope选项,此行为类似于属性。 但您可以通过以下方式扩展它:query_options。 什么是内部查询选项将在查询链中调用。 所以在内部它将被调用如下:

 Booking.confirmed_scope.where("starts_at > 18-02-2014 AND ends_at < 20-02-2014 AND studio_id = 1") 

现在更清楚了吗?