diff --git a/app/models/mixins/cloud_tenancy_mixin.rb b/app/models/mixins/cloud_tenancy_mixin.rb index 75313422f477..e57306b6699f 100644 --- a/app/models/mixins/cloud_tenancy_mixin.rb +++ b/app/models/mixins/cloud_tenancy_mixin.rb @@ -1,6 +1,11 @@ module CloudTenancyMixin extend ActiveSupport::Concern + QUERY_REFERENCES = { + :cloud_tenant => "source_tenant", + :ext_management_system => {} + }.freeze + module ClassMethods include TenancyCommonMixin @@ -13,7 +18,8 @@ def tenant_id_clause_format(tenant_ids) end def tenant_joins_clause(scope) - scope.includes(:cloud_tenant => "source_tenant").includes(:ext_management_system) + scope.includes(QUERY_REFERENCES) + .references(QUERY_REFERENCES) # needed for the where to work end end end diff --git a/lib/rbac/filterer.rb b/lib/rbac/filterer.rb index d5ad0e563867..b6c24c6e1f0a 100644 --- a/lib/rbac/filterer.rb +++ b/lib/rbac/filterer.rb @@ -240,6 +240,7 @@ def search(options = {}) exp_sql, exp_includes, exp_attrs = search_filter.to_sql(tz) if search_filter && !klass.try(:instances_are_derived?) attrs[:apply_limit_in_sql] = (exp_attrs.nil? || exp_attrs[:supported_by_sql]) && user_filters["belongsto"].blank? + skip_references = skip_references?(options, attrs) # for belongs_to filters, scope_targets uses scope to make queries. want to remove limits for those. # if you note, the limits are put back into scope a few lines down from here @@ -249,7 +250,7 @@ def search(options = {}) .includes(include_for_find).includes(exp_includes) .order(order) - scope = include_references(scope, klass, include_for_find, exp_includes) + scope = include_references(scope, klass, include_for_find, exp_includes, skip_references) scope = scope.limit(limit).offset(offset) if attrs[:apply_limit_in_sql] targets = scope @@ -283,7 +284,12 @@ def is_sti?(klass) klass.respond_to?(:finder_needs_type_condition?) ? klass.finder_needs_type_condition? : false end - def include_references(scope, klass, include_for_find, exp_includes) + def skip_references?(options, attrs) + options[:extra_cols].blank? && !attrs[:apply_limit_in_sql] + end + + def include_references(scope, klass, include_for_find, exp_includes, skip) + return scope if skip ref_includes = Hash(include_for_find).merge(Hash(exp_includes)) unless polymorphic_include?(klass, ref_includes) scope = scope.references(include_for_find).references(exp_includes)