Skip to content

Commit 3e371c6

Browse files
committed
Fix issue with IDs reader on preloaded associations for composite primary keys
1 parent 3769f1e commit 3e371c6

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Fix an issue where the IDs reader method did not return expected results
2+
for preloaded associations in models using composite primary keys.
3+
4+
*Jay Ang*
5+
16
* Allow to configure `strict_loading_mode` globally or within a model.
27

38
Defaults to `:all`, can be changed to `:n_plus_one_only`.

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ def writer(records)
5050
# Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items
5151
def ids_reader
5252
if loaded?
53-
target.pluck(reflection.association_primary_key)
53+
target.pluck(*reflection.association_primary_key)
5454
elsif !target.empty?
55-
load_target.pluck(reflection.association_primary_key)
55+
load_target.pluck(*reflection.association_primary_key)
5656
else
57-
@association_ids ||= scope.pluck(reflection.association_primary_key)
57+
@association_ids ||= scope.pluck(*reflection.association_primary_key)
5858
end
5959
end
6060

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3234,6 +3234,12 @@ def test_key_ensuring_owner_was_is_valid_when_dependent_option_is_destroy_async
32343234
MESSAGE
32353235
end
32363236

3237+
def test_ids_reader_on_preloaded_association_with_composite_primary_key
3238+
great_author = cpk_authors(:cpk_great_author)
3239+
3240+
assert_equal great_author.books.ids, Cpk::Author.preload(:books).find(great_author.id).book_ids
3241+
end
3242+
32373243
private
32383244
def force_signal37_to_load_all_clients_of_firm
32393245
companies(:first_firm).clients_of_firm.load_target

0 commit comments

Comments
 (0)