Skip to content

Commit

Permalink
Clean up old composite key code
Browse files Browse the repository at this point in the history
Because where now supports tuple querying, we don't need to manually
build queries with where and or statements.
  • Loading branch information
gmcgibbon committed Sep 15, 2023
1 parent 83f36d5 commit 1111ec0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def ids_writer(ids)
ids.map! { |id| pk_type.cast(id) }

records = if klass.composite_primary_key?
query_records = ids.map { |values_set| klass.where(primary_key.zip(values_set).to_h) }.inject(&:or)

query_records.index_by do |record|
klass.where(primary_key => ids).index_by do |record|
primary_key.map { |primary_key| record._read_attribute(primary_key) }
end
else
Expand Down
13 changes: 2 additions & 11 deletions activerecord/lib/active_record/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,7 @@ def find_one(id)
def find_some(ids)
return find_some_ordered(ids) unless order_values.present?

relation = if klass.composite_primary_key?
ids.map { |values_set| where(primary_key.zip(values_set).to_h) }.inject(&:or)
else
where(primary_key => ids)
end

relation = where(primary_key => ids)
relation = relation.select(table[primary_key]) unless select_values.empty?
result = relation.to_a

Expand All @@ -559,11 +554,7 @@ def find_some_ordered(ids)
ids = ids.slice(offset_value || 0, limit_value || ids.size) || []

relation = except(:limit, :offset)
relation = if klass.composite_primary_key?
ids.map { |values_set| relation.where(primary_key.zip(values_set).to_h) }.inject(&:or)
else
relation.where(primary_key => ids)
end
relation = relation.where(primary_key => ids)
relation = relation.select(table[primary_key]) unless select_values.empty?
result = relation.records

Expand Down

0 comments on commit 1111ec0

Please sign in to comment.