Fix scopes combined with OR combinator not working #1622
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes an issue where scopes combined with the
:or
combinator would not generate proper OR logic in SQL queries.Problem
When using scopes with the OR combinator, the generated SQL would ignore the OR logic and apply scopes with AND semantics instead:
Root Cause
Scopes were applied immediately during search building via
@context.chain_scope()
, bypassing the visitor pattern that handles OR/AND combinator logic. This meant scopes were always chained with AND semantics regardless of the specified combinator.Solution
Two-part minimal fix:
.or()
methodThe fix handles:
Person.ransack(red: true, green: true, m: :or)
Person.ransack(red: true, name_cont: 'John', m: :or)
Person.ransack(red: true, green: true)
Changes Made
lib/ransack/search.rb
: Modifiedadd_scope
to defer scope application when OR combinator is usedlib/ransack/adapters/active_record/context.rb
: Enhancedevaluate
method to handle scope OR combinationsspec/ransack/scope_or_combinator_spec.rb
: Added comprehensive test coverageBackward Compatibility
This change is fully backward compatible. The default AND behavior remains completely unchanged, and the fix only activates when both OR combinator and scopes are present.
Closes #[issue_number]
Additional instructions:
Fixes #1110
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.