Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 24, 2025

Summary

Fixes a bug in extract_correlated_key that caused NoMethodError: undefined method 'eq' for nil when building queries with polymorphic associations and complex join conditions.

Problem

The extract_correlated_key method only checked the left and right properties of Arel::Nodes::And nodes:

when Arel::Nodes::And
  extract_correlated_key(join_root.left) || extract_correlated_key(join_root.right)

However, Arel::Nodes::And can have more than 2 children stored in the children array. When Rails builds complex queries with polymorphic associations and default scopes, it creates And nodes with multiple conditions. If the matching equality condition containing the primary key correlation was in the third or later position, extract_correlated_key would return nil, causing the error when trying to call correlated_key.eq(primary_key).

Reproduction

This issue typically occurred with queries like:

# Models with polymorphic associations and default scopes
class Article < ActiveRecord::Base
  has_many :recent_notes, as: :notable
  default_scope { where(published: true) }
end

class RecentNote < ActiveRecord::Base
  self.table_name = "notes"
  default_scope { where(notable_id: 1) }
  belongs_to :notable, polymorphic: true
end

# This would fail with NoMethodError
Article.ransack(recent_notes_note_not_cont_all: "test").result.to_sql

Solution

Updated the method to iterate through all children of And nodes instead of only checking the first two:

when Arel::Nodes::And
  # And may have multiple children, so we need to check all, not just left/right
  join_root.children.each do |child|
    key = extract_correlated_key(child)
    return key if key
  end
  nil

Changes

  • Fixed extract_correlated_key method in lib/ransack/adapters/active_record/context.rb
  • Added comprehensive test coverage for the fix
  • Updated .gitignore to exclude vendor/bundle

Testing

  • Added targeted tests that reproduce the bug and verify the fix
  • All existing tests continue to pass (415 examples, 0 failures)
  • Verified the original bug scenario now works correctly
  • Tested complex polymorphic associations with multiple join conditions

This is a minimal, surgical fix that maintains full backward compatibility while resolving the issue for users encountering this error with complex query scenarios.


Additional instructions:

  • ensure adequate test cases are created
  • take the opportunity to refactor where possible

Fixes #1571


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits September 24, 2025 13:56
…hildren

- Updated extract_correlated_key method to iterate through all children of And nodes instead of only checking left/right
- Added comprehensive test coverage for the fix
- Verified fix works with polymorphic associations and complex join conditions
- All existing tests continue to pass

Co-authored-by: scarroll32 <[email protected]>
@Copilot Copilot AI changed the title [WIP] extract_correlated_key incorrectly handles Arel::Nodes::And nodes with multiple children Fix extract_correlated_key to handle Arel::Nodes::And with multiple children Sep 24, 2025
@Copilot Copilot AI requested a review from scarroll32 September 24, 2025 14:04
Copilot finished work on behalf of scarroll32 September 24, 2025 14:04
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.

extract_correlated_key incorrectly handles Arel::Nodes::And nodes with multiple children
2 participants