Skip to content

Commit

Permalink
Use a different variable name to avoid overriding the custom table_al…
Browse files Browse the repository at this point in the history
…ias_name
  • Loading branch information
Arthur Chui committed May 7, 2020
1 parent b51e414 commit 34266dc
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/jit_preloader/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,24 @@ def has_many_aggregate(assoc, name, aggregate, field, table_alias_name: nil, def
association_scope = association_scope.instance_exec(&reflection.scope).reorder(nil) if reflection.scope

# If the query uses an alias for the association, use that instead of the table name
table_alias_name ||= association_scope.references_values.first || aggregate_association.table_name
table_reference = table_alias_name
table_reference ||= association_scope.references_values.first || aggregate_association.table_name

conditions[table_alias_name] = { aggregate_association.foreign_key => primary_ids }
conditions[table_reference] = { aggregate_association.foreign_key => primary_ids }

# If the association is a STI child model, specify its type in the condition so that it
# doesn't include results from other child models
parent_is_base_class = aggregate_association.klass.superclass.abstract_class? || aggregate_association.klass.superclass == ActiveRecord::Base
has_type_column = aggregate_association.klass.column_names.include?(aggregate_association.klass.inheritance_column)
is_child_sti_model = !parent_is_base_class && has_type_column
if is_child_sti_model
conditions[table_alias_name].merge!({ aggregate_association.klass.inheritance_column => aggregate_association.klass.sti_name })
conditions[table_reference].merge!({ aggregate_association.klass.inheritance_column => aggregate_association.klass.sti_name })
end

if reflection.type.present?
conditions[reflection.type] = self.class.name
end
group_by = "#{table_alias_name}.#{aggregate_association.foreign_key}"
group_by = "#{table_reference}.#{aggregate_association.foreign_key}"

preloaded_data = Hash[association_scope
.where(conditions)
Expand Down

0 comments on commit 34266dc

Please sign in to comment.