-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Fix: Incorrectly preloading through association records when middle association has been loaded #46579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Incorrectly preloading through association records when middle association has been loaded #46579
Conversation
54d8dd8
to
1fd3021
Compare
1fd3021
to
3758d99
Compare
b657cbe
to
c540b36
Compare
498cbc2
to
fc76bf3
Compare
end | ||
|
||
assert_no_queries do | ||
assert_equal expected_member_detail_ids.sort, member.organization_member_details_2.map(&:id).sort |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
member.reload.organization # load through record | ||
|
||
assert_queries(1) do | ||
ActiveRecord::Associations::Preloader.new(records: [member], associations: :organization_member_details_2).call |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fc76bf3
to
5eb9443
Compare
5eb9443
to
85e404e
Compare
85e404e
to
18f8335
Compare
FWIW, I've confirmed this fixes the issue described in #45542, but I'm not authoritative enough to say whether or not it's a real bug. |
This looks great! Thanks! |
d472cb9
to
4ab9e84
Compare
… when middle association has been loaded
4ab9e84
to
0178e1d
Compare
Cheers! @byroot |
Hi, Thanks for the fix, this issue was blocking application I work on from upgrading to Rails 7.0. If I understand correctly how rails releases work, this fix will be included in Rails 7.1 release as it was merged in |
…rrect-preloading Fix: Incorrectly preloading through association records when middle association has been loaded
I just backported it in 7-0-stable as 99ee8bd. You can point your Gemfile at this branch. |
Thanks a lot! |
Fix: #45822
This Pull Request has been created to address #45822.
Update: Looks like this also resolves #45542.
Detail
This Pull Request fixes
ActiveRecord::Associations::Preloader::ThroughAssociation#records_by_owner
to:RE: 1
Using a failing test and some logging, I found that the memoized keys in
source_records_by_owner
had differentobject_id
s to the values inthrough_records_by_owner
at the point at which the value was fetched usingsource_records_by_owner[record]
and was therefore returningnil
.This was being caused by
def middle_records
loading new objects into@preloaded_records
which would get passed in as the records for the source preloader, rather than using the already loaded through objects inthrough_records_by_owner
.This resulted in the
source_records_by_owner
hash containing keys which were different objects to the values in thethrough_records_by_owner
hash.RE: 2
This also happens to fix a circular dependency of sorts, since
def middle_records
would eventually calldef source_preloaders
while already in a previous call to it originating fromdef source_records_by_owner
, causing the through record/s to reload and the source record/s to load, before finally loading the source records once again. These extra queries are visible in my comment below.Additional information
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]