This blog is part of our Rails 6 series.
Rails 6.0 was recently released.
A notable deprecation warning has been added
in Rails 6 when using
with multiple attributes.
Before Rails 6,
if we use
where.not with multiple attributes,
it applies logical
NOR (NOT(A) AND NOT(B))
in WHERE clause of the query.
This does not always work as expected.
Let’s look at an example to understand this better.
with a polymorphic association.
In the last query,
we expect ActiveRecord to fetch one record.
Let’s check SQL generated for the above case.
where.not applies AND to the
and fails to fetch expected records.
In such cases,
correct implementation of
logical NAND (NOT(A) OR NOT(B)).
Let us query
posts table using NAND this time.
Above query works as expected and returns one record.
Rails 6.1 will change
where.not working to NAND
similar to the above query.
It is well mentioned in deprecation warning
that if we wish to use NOR condition
with multiple attributes,
we can chain multiple
using a single predicate.
Here’s the relevant
for this change.