File tree Expand file tree Collapse file tree 2 files changed +12
-3
lines changed
lib/active_record/relation Expand file tree Collapse file tree 2 files changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -325,19 +325,22 @@ def async_pick(*column_names)
325
325
# Person.ids # SELECT people.id FROM people
326
326
# Person.joins(:companies).ids # SELECT people.id FROM people INNER JOIN companies ON companies.id = people.company_id
327
327
def ids
328
+ primary_key_array = Array ( primary_key )
329
+
328
330
if loaded?
329
- result = records . pluck ( *Array ( primary_key ) )
331
+ result = records . pluck ( *primary_key_array )
330
332
return @async ? Promise ::Complete . new ( result ) : result
331
333
end
332
334
333
335
if has_include? ( primary_key )
334
- relation = apply_join_dependency . distinct
336
+ relation = apply_join_dependency . group ( * primary_key_array )
335
337
return relation . ids
336
338
end
337
339
338
- columns = arel_columns ( Array ( primary_key ) )
340
+ columns = arel_columns ( primary_key_array )
339
341
relation = spawn
340
342
relation . select_values = columns
343
+
341
344
result = if relation . where_clause . contradiction?
342
345
ActiveRecord ::Result . empty
343
346
else
Original file line number Diff line number Diff line change @@ -1049,6 +1049,12 @@ def test_ids_with_includes
1049
1049
assert_equal Company . all . map ( &:id ) . sort , Company . all . includes ( :contracts ) . ids . sort
1050
1050
end
1051
1051
1052
+ def test_ids_with_includes_and_non_primary_key_order
1053
+ rating = 1
1054
+ Company . all . each { |company | company . update! ( rating : rating += 1 ) }
1055
+ assert_equal Company . all . sort_by ( &:rating ) . map ( &:id ) , Company . includes ( :comments ) . order ( :rating ) . ids
1056
+ end
1057
+
1052
1058
def test_ids_with_includes_and_scope
1053
1059
scoped_ids = [ 1 , 2 ]
1054
1060
company = Company . where ( id : scoped_ids ) . first
You can’t perform that action at this time.
0 commit comments