Skip to content
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 #51524] Fix strict_loading violations ignored when using pluck #51627

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

shes50103
Copy link
Contributor

@shes50103 shes50103 commented Apr 22, 2024

Motivation / Background

Fixes #51524

Detail

  • Update ActiveRecord::Calculations#pluck to raise an error or log on a strict loading violation.
  • Update the ActiveRecord::Associations::Association#violates_strict_loading? make preloaded association won't violates strict loading

Additional information

This PR will conflict with #50389 as we are fixing a similar issue in a different scenario.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@@ -210,6 +210,18 @@ def create!(attributes = nil, &block)
_create_record(attributes, true, &block)
end

def violates_strict_loading?
return unless find_target?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the find_target? check to ensure that the loaded association adheres to the strict loading rule, which is necessary for passing the newly added test, test_preload_audit_logs_are_strict_loading_with_pluck.

Previously, the violates_strict_loading? method functioned correctly without this check because find_target? was used earlier in the process.

if find_target?
@target = merge_target_lists(find_target, target)
end

def find_target
if violates_strict_loading?
Base.strict_loading_violation!(owner: owner.class, reflection: reflection)
end

I'm not sure whether to open a separate PR for this refactoring since it's a minor change. For now, I've added it as a second commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant