Skip to content

Commit ef02c3a

Browse files
authored
Merge pull request rails#46697 from joshuay03/improve-#ids-test-coverage
Update `ActiveRecord::Calculations#ids` and improve test coverage
2 parents 01bc3a4 + 0c16feb commit ef02c3a

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

activerecord/lib/active_record/relation/calculations.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ def ids
327327
return @async ? Promise::Complete.new(result) : result
328328
end
329329

330+
if has_include?(primary_key)
331+
relation = apply_join_dependency.distinct
332+
return relation.ids
333+
end
334+
330335
columns = arel_columns([primary_key])
331336
relation = spawn
332337
relation.select_values = columns

activerecord/test/cases/calculations_test.rb

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,12 @@ def test_ids_with_scope
937937
assert_equal Company.where(id: scoped_ids).map(&:id).sort, Company.where(id: scoped_ids).ids.sort
938938
end
939939

940+
def test_ids_on_relation
941+
company = Company.first
942+
contract = company.contracts.create!
943+
assert_equal [contract.id], company.contracts.ids
944+
end
945+
940946
def test_ids_on_loaded_relation
941947
loaded_companies = Company.all.load
942948
company_ids = Company.all.map(&:id)
@@ -968,6 +974,20 @@ def test_ids_with_contradicting_scope
968974
end
969975
end
970976

977+
def test_ids_with_join
978+
company = Company.first
979+
company.contracts.create!
980+
assert_equal [company.id], Company.joins(:contracts).where("contracts.id" => company.contracts.first).ids
981+
end
982+
983+
def test_ids_with_polymorphic_relation_join
984+
part = ShipPart.create!(name: "has trinket")
985+
part.trinkets.create!
986+
987+
assert_equal [part.id], ShipPart.joins(:trinkets).ids
988+
assert_async_equal [part.id], ShipPart.joins(:trinkets).async_ids
989+
end
990+
971991
def test_ids_with_eager_load
972992
company = Company.first
973993
5.times { company.contracts.create! }
@@ -986,11 +1006,36 @@ def test_ids_with_includes
9861006
assert_equal Company.all.map(&:id).sort, Company.all.includes(:contracts).ids.sort
9871007
end
9881008

989-
def test_ids_with_scope_and_includes
1009+
def test_ids_with_includes_and_scope
9901010
scoped_ids = [1, 2]
9911011
company = Company.where(id: scoped_ids).first
9921012
5.times { company.contracts.create! }
993-
assert_equal Company.where(id: scoped_ids).map(&:id).sort, Company.where(id: scoped_ids).includes(:contracts).ids.sort
1013+
assert_equal Company.where(id: scoped_ids).map(&:id).sort, Company.includes(:contracts).where(id: scoped_ids).ids.sort
1014+
end
1015+
1016+
def test_ids_with_includes_and_table_scope
1017+
company = Company.first
1018+
company.contracts.create!
1019+
assert_equal [company.id], Company.includes(:contracts).where("contracts.id" => company.contracts.first).ids
1020+
end
1021+
1022+
def test_ids_on_loaded_relation_with_includes_and_table_scope
1023+
company = Company.first
1024+
company.contracts.create!
1025+
loaded_companies = Company.includes(:contracts).where("contracts.id" => company.contracts.first).load
1026+
assert_queries(0) do
1027+
assert_equal [company.id], loaded_companies.ids
1028+
end
1029+
end
1030+
1031+
def test_ids_with_includes_limit_and_empty_result
1032+
assert_equal [], Topic.includes(:replies).limit(0).ids
1033+
assert_equal [], Topic.includes(:replies).limit(1).where("0 = 1").ids
1034+
end
1035+
1036+
def test_ids_with_includes_offset
1037+
assert_equal [5], Topic.includes(:replies).order(:id).offset(4).ids
1038+
assert_equal [], Topic.includes(:replies).order(:id).offset(5).ids
9941039
end
9951040

9961041
def test_pluck_with_includes_limit_and_empty_result
@@ -1499,6 +1544,20 @@ def test_count_with_block_and_column_name_raises_an_error
14991544
end
15001545
end
15011546

1547+
test "#skip_query_cache! for #ids" do
1548+
Account.cache do
1549+
assert_queries(1) do
1550+
Account.ids
1551+
Account.ids
1552+
end
1553+
1554+
assert_queries(2) do
1555+
Account.all.skip_query_cache!.ids
1556+
Account.all.skip_query_cache!.ids
1557+
end
1558+
end
1559+
end
1560+
15021561
test "#skip_query_cache! for a simple calculation" do
15031562
Account.cache do
15041563
assert_queries(1) do

0 commit comments

Comments
 (0)