From a01c89ce3accdf2bc8a1c60897426ead945adc24 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Thu, 14 Nov 2024 04:28:20 +0100 Subject: [PATCH] Further simplify EXISTS condition with SelectManager --- app/models/unit.rb | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/app/models/unit.rb b/app/models/unit.rb index 96beb7e..5abe06d 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -21,19 +21,16 @@ class Unit < ApplicationRecord # add 'portable' fields (import on !default == export) to select with_defaults - .where("NOT EXISTS (?)", - Unit.select(1).from(other_units).joins( - arel_table.create_join( - other_bases_units, - arel_table.create_on(other_bases_units[:id].eq(other_units[:base_id])), - Arel::Nodes::OuterJoin - ) - ).where( - other_bases_units[:symbol].eq(Arel::Table.new(:bases_units)[:symbol]) - .and(other_units[:symbol].eq(arel_table[:symbol])) - .and(other_units[:user_id].not_eq(arel_table[:user_id])) - ) - ) + .where.not( + Arel::SelectManager.new.from(other_units) + .outer_join(other_bases_units) + .on(other_units[:base_id].eq(other_bases_units[:id])) + .where( + other_bases_units[:symbol].eq(Arel::Table.new(:bases_units)[:symbol]) + .and(other_units[:symbol].eq(arel_table[:symbol])) + .and(other_units[:user_id].not_eq(arel_table[:user_id])) + ).project(1).exists + ) } scope :ordered, ->{ left_outer_joins(:base)