Skip to content

Commit

Permalink
Merge pull request #615 from henriquetatagiba/master
Browse files Browse the repository at this point in the history
Fix scope overriding join relation query
  • Loading branch information
cfis authored Dec 19, 2024
2 parents 4d5b393 + f019507 commit bb1b2fc
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/composite_primary_keys/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ def join_scope(table, foreign_table, foreign_klass)
scope_chain_items = join_scopes(table, predicate_builder)
klass_scope = klass_join_scope(table, predicate_builder)

if type
klass_scope.where!(type => foreign_klass.polymorphic_name)
end

scope_chain_items.inject(klass_scope, &:merge!)

key = join_primary_key
foreign_key = join_foreign_key

Expand All @@ -14,15 +20,11 @@ def join_scope(table, foreign_table, foreign_klass)
constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
klass_scope.where!(constraint)

if type
klass_scope.where!(type => foreign_klass.polymorphic_name)
end

if klass.finder_needs_type_condition?
klass_scope.where!(klass.send(:type_condition, table))
end

scope_chain_items.inject(klass_scope, &:merge!)
klass_scope
end
end

Expand Down

0 comments on commit bb1b2fc

Please sign in to comment.