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
ActiveRecord#includes gives different results if the includes order is different. #41378
Comments
I do confirm it's an issue, here you find a simple reproduction script (FF to add it to the issue description if you like). I didn't see any existing PRs for this: I'll work on a fix. |
I've narrowed a bit the issue and I think it's related to the memoizing in
Which doesn't work well when you have a scope in an has_many_through and the through_relation has already been preloaded without using the scope.
I'll try to figure out how to improve this check. |
@kamipo #41489 fixes this issue, could you be so kind to take a look at it? |
gem 'rails', git: "https://github.com/intrip/rails.git", branch: "41378-fix-preload-scoped" Thanks a million! |
You're welcome! |
@github0013 could you please reopen? I'd wait for the PR to be accepted/merged before closing the issue 🙂 |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
any updates? |
#41489 is still waiting for review sorry |
hello there, We also spotted this issue in rails 6.0.3.6 and we found the next workarounds. I know that you already prepared a fix which we can use a patch but might be it helpful: class A < ApplicationRecord
has_many :bs
has_many :__bs # by using it you can avoid this issue
has_many :cs, through: :bs, source: "cs"
has_many :c_type_xs, -> { type_x }, through: :__bs, source: "cs"
end |
@Reedlee |
The reason it works is because going through |
Hello @github0013 class A < ApplicationRecord
has_many :bs
has_many :__bs, class_name: "B" # by using it you can avoid this issue. Also, you can use any name instead of __bs
has_many :cs, through: :bs, source: "cs"
has_many :c_type_xs, -> { type_x }, through: :__bs, source: "cs"
end Hello @intrip |
@Reedlee If I recall correctly |
@intrip hey, Sorry for the late response. 🤔 It's interesting that this problem with order exists for 5.2.x, 6.0.x. but 6.1.x doesn't have. Also important to say that the problem with ordering exists if we are trying to get value from cs by filtering them by using bs, please, take a look at class A < ApplicationRecord
has_many :bs
has_many :cs, through: :bs, source: "cs"
# doesn't have the problem related to ordering for eager_load
has_many :c_type_xs, -> { type_x }, through: :bs, source: "cs"
# has the problem related to ordering for eager_load
has_many :c_of_by, -> { where(bs: {name: 'y'}) }, through: :bs, source: "cs"
end So if we are running cases like: |
Previously when the same `through_relation` was preloaded multiple times but using a different scope the relation was incorrectly cached; this led into returning the wrong results. The issue is fixed by checking if the relation to preload has a `preload_scope` and is preloaded from a `through_relation`. Fixes rails#41378
Steps to reproduce
I prepared a repo.
https://github.com/github0013/activerecord-includes
Expected behavior
Switching the order in
#includes
should not change the results. (please see the repo):c_type_xs, :ds
).find(subject.id).c_type_xs.size # => 1:ds, :c_type_xs
).find(subject.id).c_type_xs.size # => 1Actual behavior
:c_type_xs, :ds
).find(subject.id).c_type_xs.size # => 1:ds, :c_type_xs
).find(subject.id).c_type_xs.size # => 4System configuration
Rails version:
Ruby version:
The text was updated successfully, but these errors were encountered: