Skip to content

Commit

Permalink
Fix issue with IDs reader on preloaded associations for composite pri…
Browse files Browse the repository at this point in the history
…mary keys
  • Loading branch information
Jay0921 committed Apr 5, 2024
1 parent 84c4598 commit dcf7984
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Fix an issue where the IDs reader method did not return expected results for preloaded associations
in models using composite primary keys.

*Jay Ang*

* Raise an `ActiveRecord::ActiveRecordError` error when the MySQL database returns an invalid version string.

*Kevin McPhillips*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ def writer(records)
# Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items
def ids_reader
if loaded?
target.pluck(reflection.association_primary_key)
target.pluck(*reflection.association_primary_key)
elsif !target.empty?
load_target.pluck(reflection.association_primary_key)
load_target.pluck(*reflection.association_primary_key)
else
@association_ids ||= scope.pluck(reflection.association_primary_key)
@association_ids ||= scope.pluck(*reflection.association_primary_key)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3232,6 +3232,12 @@ def test_key_ensuring_owner_was_is_valid_when_dependent_option_is_destroy_async
MESSAGE
end

def test_ids_reader_on_preloaded_association_with_composite_primary_key
great_author = cpk_authors(:cpk_great_author)

assert_equal great_author.books.ids, Cpk::Author.preload(:books).find(great_author.id).book_ids
end

private
def force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.load_target
Expand Down

0 comments on commit dcf7984

Please sign in to comment.