Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

jbhannah/invert_where_test

Repository files navigation

Testing #invert_where vs. explicit scope inversion

#invert_where inverts all preceding #where clauses in an ActiveRecord query chain, including those contained within scopes. Instead, retrieving and inverting the [Arel::SelectManager#constraints][constr] of just the desired scope allows for order-independent scope chaining without side effects.

Given the scopes defined in theFoobar model:

Foobar.active.published
# SELECT "foobars".* FROM "foobars" WHERE "foobars"."expires_at" >= '2021-12-10 04:21:48.044576' AND "foobars"."published" = 1

Foobar.published.active
# SELECT "foobars".* FROM "foobars" WHERE "foobars"."published" = 1 AND "foobars"."expires_at" >= '2021-12-10 04:21:48.044576'

Foobar.expired_inverse_of.published
# SELECT "foobars".* FROM "foobars" WHERE NOT ("foobars"."expires_at" >= '2021-12-10 04:21:48.044576') AND "foobars"."published" = 1

Foobar.published.expired_inverse_of
# SELECT "foobars".* FROM "foobars" WHERE "foobars"."published" = 1 AND NOT ("foobars"."expires_at" >= '2021-12-10 04:21:48.044576')

Foobar.expired_invert_where.published
# SELECT "foobars".* FROM "foobars" WHERE "foobars"."expires_at" < '2021-12-10 04:21:48.044576' AND "foobars"."published" = 1

Foobar.published.expired_invert_where
# SELECT "foobars".* FROM "foobars" WHERE NOT ("foobars"."published" = 1 AND "foobars"."expires_at" >= '2021-12-10 04:21:48.044576')