Skip to content

Commit

Permalink
Extend composite primary key support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeiemam authored and MaxLap committed Aug 29, 2024
1 parent cb65189 commit 7b62db1
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/active_record_where_assoc/core_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,13 @@ def self.build_alias_scope_for_recursive_association(reflection, poly_belongs_to

alias_scope = foreign_klass.base_class.unscoped
alias_scope = alias_scope.from("#{table.name} #{ALIAS_TABLE.name}")
alias_scope = alias_scope.where(table[primary_key].eq(ALIAS_TABLE[primary_key]))

Array.wrap(primary_key).each_with_index do |a_primary_key, i|
a_constraint = table[a_primary_key].eq(ALIAS_TABLE[a_primary_key])
the_operation = i == 0 ? :where : :and
alias_scope = alias_scope.send(the_operation, a_constraint)
end

alias_scope
end

Expand Down Expand Up @@ -430,9 +436,9 @@ def self.wrapper_and_join_constraints(record_class, reflection, options = {})
constraint_key_map = constraint_keys.zip(constraint_foreign_keys)

constraint_key_map.each do |primary_and_foreign_keys|
the_primary_key, the_foreign_key = primary_and_foreign_keys
the_constraint = table[the_primary_key].eq(foreign_table[the_foreign_key])
constraints = constraints ? constraints.and(the_constraint) : the_constraint
a_primary_key, a_foreign_key = primary_and_foreign_keys
a_constraint = table[a_primary_key].eq(foreign_table[a_foreign_key])
constraints = constraints ? constraints.and(a_constraint) : a_constraint
end

if reflection.type
Expand Down

0 comments on commit 7b62db1

Please sign in to comment.