Validates_Overlap宝石多个范围覆盖海誓山盟 [英] Validates_Overlap Gem Multiple Scopes Overwriting Eachother
问题描述
我使用Validates_Overlap宝石可以在这里找到: https://github.com/robinbortlik/validates_overlap
I'm using Validates_Overlap Gem which can be found here: https://github.com/robinbortlik/validates_overlap
的实质是,我有两个房间可以预订。我希望在同一个房间里已经在同一个房间确认的预订确认介入。它不应该把我的错误,当其他房间被黄牌警告,或者同一个房间被黄牌警告,但尚未得到证实。
The essence is I have two rooms that can be booked. I want the validation to step in when the same room already has a CONFIRMED booking in the SAME room. It shouldn't throw me an error when the other room is booked, or if the same room is booked but hasn't been confirmed.
我的code到目前为止如下:
My code so far is as follows
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
这将返回从我的服务器如下:
This returns the following from my server:
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),其中没有一个被确认。是什么给了?
There are two other bookings (with this studio_id) and none of them are confirmed. What gives?
下面都与预订:studio_id => 2
Here are all the bookings with :studio_id => 2
[#<Booking id: 1, studio_id: 2, engineer_id: 5, is_confirmed: false, title: "", allDay: false, created_at: "2014-10-23 19:59:01", updated_at: "2014-10-23 19:59:01", start_time: "2014-10-23 19:00:00", end_time: "2014-10-23 21:00:00", user_id: nil, booker: "Client", client_id: 3>,
#<Booking id: 8, studio_id: 2, engineer_id: 1, is_confirmed: false, title: "", allDay: false, created_at: "2014-10-24 03:07:34", updated_at: "2014-10-24 03:07:34", start_time: "2014-10-23 19:00:00", end_time: "2014-10-23 22:00:00", user_id: nil, booker: "Pat Sullivan", client_id: nil>,
#<Booking id: 9, studio_id: 2, engineer_id: 2, is_confirmed: false, title: "", allDay: false, created_at: "2014-10-24 03:26:17", updated_at: "2014-10-24 03:26:17", start_time: "2014-10-23 20:00:00", end_time: "2014-10-24 03:00:00", user_id: nil, booker: "Client", client_id: 4>]
更新的我注意到,studio_id是不被人注意的&放大器;&安培;在范围线。我怎样才能让示波器注册两者兼而有之?我能做到的范围内行或者我应该创建一个方法?
Update I noticed that the studio_id isn't being noticed with the && in the scope line. How can I have the scope register both? Can I do it within the scope line or should I create a method?
我也试了一个简单的
validates :start_time, :end_time,
:overlap => {
:exclude_edges => ["starts_at", "ends_at"],
:scope => "is_confirmed" && "studio_id"
}, on: :update
这做同样的事情 - 只用后来的studio_id
This does the same thing -- only uses the later "studio_id"
推荐答案
我知道,期权的名称混乱,我很抱歉这一点。
I know, that the names of options are confusing and I'm sorry for that.
我建议你实现你的命名范围称为:确认并把它作为:query_option参数
I suggest you to implement your named scope called :confirmed and pass it as :query_option parameter.
我想,这应该是这样的:
I think, it should look like this:
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
简短说明:您传递为:作用域选项,这种行为类似于属性。但是你可以扩展它:query_options。什么是里面的查询选项将被称为查询链。因此,在内部它被称为是这样的:
Short explanation: what you pass as a :scope option, this behave like attribute. But you can extend it by :query_options. What is inside query options will be called in the query chain. So internally it will be called like this:
Booking.confirmed_scope.where("starts_at > 18-02-2014 AND ends_at < 20-02-2014 AND studio_id = 1")
是不是更清楚了吧?
Is it more clear now?
这篇关于Validates_Overlap宝石多个范围覆盖海誓山盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!