Skip to content

Conversation

riseshia
Copy link
Contributor

🔗 #1571

This is proposed patch for #1571. Check issue for the detail of this.

@riseshia riseshia force-pushed the fix-extract-correlated-key-with-and-node-has-multiple-children branch from 365c324 to e471827 Compare August 5, 2025 12:01
@riseshia
Copy link
Contributor Author

riseshia commented Aug 5, 2025

fix tests with mysql, which won't be failed e471827

@riseshia riseshia marked this pull request as ready for review August 5, 2025 12:21
@scarroll32 scarroll32 requested a review from Copilot September 24, 2025 13:45
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where correlated subqueries fail to extract the proper key when Arel::Nodes::And nodes have more than two children. The fix changes from accessing only left and right children to iterating through all children in the children array.

  • Updates the extract_correlated_key method to handle Arel::Nodes::And nodes with multiple children
  • Adds test coverage for polymorphic associations with default scopes
  • Adds a RecentNote model to support the new test case

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
lib/ransack/adapters/active_record/context.rb Updates extract_correlated_key to iterate through all children of Arel::Nodes::And nodes
spec/support/schema.rb Adds RecentNote model and recent_notes association for testing
spec/ransack/adapters/active_record/context_spec.rb Adds test case for polymorphic associations with default scopes

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

when Arel::Nodes::And
extract_correlated_key(join_root.left) || extract_correlated_key(join_root.right)
# And may have multiple children, so we need to check all, not via left/right
join_root.children.each do |child|
Copy link
Member

Choose a reason for hiding this comment

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

@riseshia what is the potential for performance issues here?

Copy link
Contributor Author

@riseshia riseshia Sep 24, 2025

Choose a reason for hiding this comment

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

no performance issue here, it just has possibility that join_root have more than 2 children.

@scarroll32 scarroll32 enabled auto-merge (squash) September 24, 2025 19:39
@scarroll32 scarroll32 merged commit 1109829 into activerecord-hackery:main Sep 24, 2025
24 checks passed
@riseshia riseshia deleted the fix-extract-correlated-key-with-and-node-has-multiple-children branch September 24, 2025 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants