Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 24, 2025

This PR adds a failing test that reproduces the double-join issue reported in the original issue where ransack creates duplicate joins when using pluck on queries that already have associations joined via includes and where.

Problem

When using the following pattern:

Visitor.includes(:automated_campaign_receipts)
  .where(automated_campaign_receipts: { automated_campaign_id: 10 })
  .ransack(automated_campaign_receipts_event_type_eq: 'clicked')
  .result.pluck(:id)

Ransack erroneously creates a double-join instead of reusing the existing join from includes and where:

Expected SQL (1 join):

SELECT "visitors"."id" FROM "visitors" 
LEFT OUTER JOIN "automated_campaign_receipts" ON "automated_campaign_receipts"."visitor_id" = "visitors"."id" 
WHERE "automated_campaign_receipts"."automated_campaign_id" = ? AND "automated_campaign_receipts"."event_type" = ?

Actual SQL (erroneous double-join):

SELECT "visitors"."id" FROM "visitors" 
LEFT OUTER JOIN "automated_campaign_receipts" ON "automated_campaign_receipts"."visitor_id" = "visitors"."id" 
LEFT OUTER JOIN "automated_campaign_receipts" "automated_campaign_receipts_visitors" ON "automated_campaign_receipts_visitors"."visitor_id" = "visitors"."id" 
WHERE "automated_campaign_receipts"."automated_campaign_id" = ? AND "automated_campaign_receipts"."event_type" = 'clicked'

Changes

  • Added test models (Visitor, AutomatedCampaign, AutomatedCampaignReceipt) to reproduce the exact scenario
  • Created spec/ransack/adapters/active_record/double_join_pluck_spec.rb with a failing test that:
    • Demonstrates the double-join issue occurs specifically when using pluck()
    • Shows that regular selects (without pluck) work correctly
    • Captures and compares the actual SQL generated by both approaches

The test currently fails as expected, providing a concrete reproduction case for this issue that can be used to verify any future fix.

Impact

This issue affects performance and can potentially cause incorrect query results when the same association is joined multiple times with different aliases.


Additional instructions:

Please create a PR with only the failing test

Fixes #1108


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

Copilot AI and others added 3 commits September 24, 2025 21:28
…ining translations (#1614)

* Initial plan

* Changes before error encountered

Co-authored-by: scarroll32 <11340230+scarroll32@users.noreply.github.com>

* Improve sorted translations docs

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: scarroll32 <11340230+scarroll32@users.noreply.github.com>
Copilot AI and others added 2 commits September 24, 2025 22:30
Co-authored-by: scarroll32 <11340230+scarroll32@users.noreply.github.com>
Co-authored-by: scarroll32 <11340230+scarroll32@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Erroneous double-join when using pluck Add failing test to reproduce erroneous double-join when using pluck with ransack Sep 24, 2025
Copilot finished work on behalf of scarroll32 September 24, 2025 22:33
@Copilot Copilot AI requested a review from scarroll32 September 24, 2025 22:33
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.

Erroneous double-join when using pluck
2 participants