Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 验证多个作用域是否相互覆盖_Ruby On Rails_Validation_Activerecord_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 验证多个作用域是否相互覆盖

Ruby on rails 验证多个作用域是否相互覆盖,ruby-on-rails,validation,activerecord,ruby-on-rails-4,Ruby On Rails,Validation,Activerecord,Ruby On Rails 4,我使用的是Validates_重叠Gem,可以在这里找到: 关键是我有两个房间可以预订。我希望在同一房间已确认同一房间的预订时进行验证。当另一个房间被预订时,或者如果同一个房间被预订但尚未确认,这不会给我带来错误 到目前为止,我的代码如下 validates :start_time, :end_time, :overlap => { :exclude_edges => ["starts_at", "ends_at"], :scope =>

我使用的是Validates_重叠Gem,可以在这里找到:

关键是我有两个房间可以预订。我希望在同一房间已确认同一房间的预订时进行验证。当另一个房间被预订时,或者如果同一个房间被预订但尚未确认,这不会给我带来错误

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

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”

我知道,选项的名称很混乱,对此我很抱歉

我建议您实现名为:confirm的命名范围,并将其作为:query\u选项参数传递

我想,应该是这样的:

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
Booking.confirmed_scope.where("starts_at > 18-02-2014 AND ends_at < 20-02-2014 AND studio_id = 1")
班级预订{已确认:真}
验证:开始时间,:结束时间,:重叠=>{
:exclude_edges=>[“开始于”,“结束于”],
:scope=>“工作室id”,
:查询\u选项=>{:已确认\u范围=>nil}
},on::更新
结束
顺便说一句。。。如果您使用的是Rails 4.1,请小心,这是一个变化

简要说明:您传递的是:scope选项,其行为类似于属性。但您可以通过以下方式对其进行扩展:query\u options。查询选项中的内容将在查询链中调用。因此,在内部,它将被称为:

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
Booking.confirmed_scope.where("starts_at > 18-02-2014 AND ends_at < 20-02-2014 AND studio_id = 1")
Booking.已确认的范围,其中(“从2014年2月18日开始,到2014年2月20日结束,工作室id=1”)

现在更清楚了吗?

很有魅力。谢谢你,罗宾!