ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Scopes combined with :or does not work

Open runephilosof opened this issue 4 years ago • 3 comments

As demonstrated in https://github.com/runephilosof/ransack-scopes-or The combinator :or does not work with scopes.

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

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

RSpec.describe Test do
  context "when conditions are two scopes" do
    let(:ransack) { Test.ransack(red: true, green: true, m: :or) }

    it "supports :or combinator" do
      expect(ransack.base.combinator).to eq :or # Success
    end

    it "generates SQL containing OR" do
      expect(ransack.result.to_sql).to include "OR" # Failure
    end
  end

  context "when conditions are a scope and an attribute" do
    let(:ransack) { Test.ransack(red: true, color_cont: "green", m: :or) }

    it "supports :or combinator" do
      expect(ransack.base.combinator).to eq :or # Success
    end

    it "generates SQL containing OR" do
      expect(ransack.result.to_sql).to include "OR" # Failure
    end
  end
end

runephilosof avatar Mar 15 '20 20:03 runephilosof

Just hit the same issue. Search with ransackable_scopes applies AND after the scope part of the query instead of OR even if m: 'or' is set.

xeron avatar Aug 18 '23 21:08 xeron

Any news about this issue?

kamilmazitov avatar Mar 14 '24 07:03 kamilmazitov