Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 24, 2025

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:

class Person < ActiveRecord::Base
  scope :red, -> { where(color: 'red') }
  scope :green, -> { where(color: 'green') }

  def self.ransackable_scopes(auth_object = nil)
    [:red, :green]
  end
end

# This would generate AND logic instead of OR
Person.ransack(red: true, green: true, m: :or).result.to_sql
# Before: SELECT * FROM people WHERE color = 'red' AND color = 'green'  (impossible)
# Expected: SELECT * FROM people WHERE (color = 'red') OR (color = 'green')

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:

  1. Defer scope application: When OR combinator is detected, don't immediately apply scopes during search building
  2. OR evaluation: During query evaluation, create separate relations for each scope and combine them using ActiveRecord's .or() method

The fix handles:

  • ✅ Multiple scopes with OR: Person.ransack(red: true, green: true, m: :or)
  • ✅ Mixed scopes and conditions: Person.ransack(red: true, name_cont: 'John', m: :or)
  • ✅ Preserves AND behavior (default): Person.ransack(red: true, green: true)
  • ✅ Respects existing scope logic: false values, arity handling, etc.
  • ✅ Edge cases: empty scopes, all-false scopes

Changes Made

  • lib/ransack/search.rb: Modified add_scope to defer scope application when OR combinator is used
  • lib/ransack/adapters/active_record/context.rb: Enhanced evaluate method to handle scope OR combinations
  • spec/ransack/scope_or_combinator_spec.rb: Added comprehensive test coverage

Backward 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:

Create a PR with the provided tests

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.

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 <[email protected]>

* Improve sorted translations docs

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: scarroll32 <[email protected]>
@Copilot Copilot AI changed the title [WIP] Scopes combined with :or does not work Fix scopes combined with OR combinator not working Sep 24, 2025
Copilot finished work on behalf of scarroll32 September 24, 2025 22:28
@Copilot Copilot AI requested a review from scarroll32 September 24, 2025 22:28
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.

Scopes combined with :or does not work
2 participants