Is there a direct way to undo a full expression where
using ActiveRecord / ARel? It seems that where.not()
given a hash of arguments, it negates each expression separately, and does not negate the whole thing with a single SQL NOT
.
Rails example
Thing.where.not(attribute1: [1,2], attribute2: [3,4], attribute3: [5,6])
Produces SQL:
select * from things where attribute1 NOT IN (1,2) AND attribute2 NOT IN (3,4) AND attribute3 NOT IN (5,6)
This is not what I'm trying to do. I want to exclude all where clause with one NOT
.
select * from things where NOT(attribute1 IN (1,2) AND attribute2 IN (3,4) AND attribute3 IN (5,6))
In Boolean notation, Rails seems to prefer to negate every component of the WHERE clause as follows:
!(a) && !(b) && !(c)
But I want to deny the whole expression:
! [ (a) && (b) && (c) ]
DeMorgan, !a || !b || !c
, ( Rails 5 or
, ). , - , , ActiveRecord ARel?
Ransack Equality Scope (, _eq), .
scope :can_find_things_eq, ->(boolean = true) {
case boolean
when true, 'true'
where(conditions_for_things)
when false, 'false'
where.not(conditions_for_things)
end
}
def self.ransackable_scopes(_auth_object = nil)
%i[can_find_things_eq]
end
Rails 5 , , ... .
where.not(attribute1: [1,2]).or(where.not(attribute2:
[3,4)).or(where.not(attribute3: [5,6))
Ors WhereNots , . where
, / DeMorgan?
!