Avoid unscoping

This commit is contained in:
cryptogopher 2024-11-15 19:45:55 +01:00
parent 4447735dce
commit d9e74ed305

View File

@ -21,7 +21,8 @@ class Unit < ApplicationRecord
other_bases_units = arel_table.alias('other_bases_units') other_bases_units = arel_table.alias('other_bases_units')
parent_units = arel_table.alias('parent_units') parent_units = arel_table.alias('parent_units')
with(units: self.with_defaults).unscope(where: :user_id).left_joins(:base) Unit.with(units: self.with_defaults).left_joins(:base)
# Exclude Units that are/have default counterpart
.where.not( .where.not(
Arel::SelectManager.new.project(1).from(other_units) Arel::SelectManager.new.project(1).from(other_units)
.outer_join(other_bases_units) .outer_join(other_bases_units)
@ -31,6 +32,7 @@ class Unit < ApplicationRecord
.and(other_units[:symbol].eq(arel_table[:symbol])) .and(other_units[:symbol].eq(arel_table[:symbol]))
.and(other_units[:user_id].not_eq(arel_table[:user_id])) .and(other_units[:user_id].not_eq(arel_table[:user_id]))
).exists ).exists
# Decide if Unit can be im-/exported based on existing hierarchy
).joins( ).joins(
arel_table.create_join(parent_units, arel_table.create_join(parent_units,
arel_table.create_on( arel_table.create_on(
@ -61,7 +63,7 @@ class Unit < ApplicationRecord
end end
def default? def default?
user.nil? user_id.nil?
end end
def exportable? def exportable?