Further simplify EXISTS condition with SelectManager

This commit is contained in:
cryptogopher 2024-11-14 04:28:20 +01:00
parent 7234d60afc
commit a01c89ce3a

View File

@ -21,19 +21,16 @@ class Unit < ApplicationRecord
# add 'portable' fields (import on !default == export) to select # add 'portable' fields (import on !default == export) to select
with_defaults with_defaults
.where("NOT EXISTS (?)", .where.not(
Unit.select(1).from(other_units).joins( Arel::SelectManager.new.from(other_units)
arel_table.create_join( .outer_join(other_bases_units)
other_bases_units, .on(other_units[:base_id].eq(other_bases_units[:id]))
arel_table.create_on(other_bases_units[:id].eq(other_units[:base_id])), .where(
Arel::Nodes::OuterJoin other_bases_units[:symbol].eq(Arel::Table.new(:bases_units)[:symbol])
) .and(other_units[:symbol].eq(arel_table[:symbol]))
).where( .and(other_units[:user_id].not_eq(arel_table[:user_id]))
other_bases_units[:symbol].eq(Arel::Table.new(:bases_units)[:symbol]) ).project(1).exists
.and(other_units[:symbol].eq(arel_table[:symbol])) )
.and(other_units[:user_id].not_eq(arel_table[:user_id]))
)
)
} }
scope :ordered, ->{ scope :ordered, ->{
left_outer_joins(:base) left_outer_joins(:base)