Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 24, 2025

This PR adds comprehensive failing tests to reproduce the polymorphic filtering inconsistency issue described in the original GitHub issue.

Problem

When using Ransack to filter records with OR conditions between polymorphic foreign keys, the query fails with:

Message.ransack(from_id_or_to_id_eq: '3d4464a4-1501-4f30-a892-fa07f72f9fa1').result.count
# => ArgumentError: Polymorphic associations do not support computing the class

This is inconsistent because:

  • Single polymorphic foreign key filtering works fine: Message.ransack(from_id_eq: '123')
  • OR conditions that include non-polymorphic fields work fine: Message.ransack(id_or_from_id_or_to_id_eq: '123')
  • The error occurs even though we're only filtering by ID values, not accessing the polymorphic associations

Test Model Added

class Message < ApplicationRecord
  belongs_to :user, class_name: 'Person'
  belongs_to :from, polymorphic: true
  belongs_to :to, polymorphic: true
end

Test Cases

Failing Cases (should work but raise errors):

  • from_id_or_to_id_eq - Core issue from GitHub report
  • to_id_or_from_id_eq - Reverse order variation
  • user_id_or_from_id_eq - Mixed regular/polymorphic FK
  • UUID string examples matching the original issue

Working Cases (demonstrate the inconsistency):

  • from_id_eq - Single polymorphic FK works
  • id_or_from_id_or_to_id_eq - Workaround mentioned in issue works
  • user_id_or_id_eq - No polymorphic FKs involved works

Files Added

  • spec/ransack/adapters/active_record/polymorphic_spec.rb - Comprehensive RSpec test suite
  • spec/support/schema.rb - Added Message model and database schema
  • spec/blueprints/messages.rb - Blueprint factory for Message model
  • test_polymorphic_issue.rb - Simple standalone test runner
  • POLYMORPHIC_ISSUE_README.md - Detailed documentation and reproduction steps

Expected Behavior

OR conditions between polymorphic foreign keys should work just like regular foreign keys since we're only filtering by ID values without needing polymorphic class computation. The behavior should be consistent with single polymorphic FK filtering that already works correctly.

These tests will serve as the foundation for implementing a fix for this inconsistency.


Additional instructions:

Please create a PR with failing tests for this scenario only

Fixes #1267


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Polymorphic filtering is inconsistent Add failing tests for polymorphic filtering inconsistency with OR conditions Sep 24, 2025
@Copilot Copilot AI requested a review from scarroll32 September 24, 2025 14:15
Copilot finished work on behalf of scarroll32 September 24, 2025 14:15
Copy link
Member

@scarroll32 scarroll32 left a comment

Choose a reason for hiding this comment

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

Tests look good, please proceed with a code fix

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.

Polymorphic filtering is inconsistent
2 participants