ransack icon indicating copy to clipboard operation
ransack copied to clipboard

does not work with null values using *_in matchers

Open siddushar opened this issue 7 years ago • 2 comments

i have requirement to search like below. where i can select multiple drop-down (ex: status as "active", "default") default status indicates that status column is empty or null.

select * from users where status in ("active","")

how can i achieve this using ransack..?

siddushar avatar Aug 07 '18 11:08 siddushar

I think you need to use *_in and *_blank, and then merge the two together.

Or you can use grouping

It would be nice if we have *_in_or_blank :smile:

fmluizao avatar Sep 17 '18 20:09 fmluizao

We had this issue as well. We want to search for items assigned to Person A, B, or unassigned.

A bit of searching found this page which has some examples of how to add this to Ransack as a new predicate. Copying the code here as well as you never know when links go offline 🙀

module Arel
  module Predications
    def eq_or_null(other)
      left  = eq(other)
      right = eq(nil)
      left.or(right)
    end

    def not_eq_or_null(other)
      left  = not_eq(other)
      right = eq(nil)
      left.or(right)
    end

    def gteq_or_null(other)
      left  = gteq(other)
      right = eq(nil)
      left.or(right)
    end

    def lt_or_null(other)
      left  = lt(other)
      right = eq(nil)
      left.or(right)
    end

    def in_or_null(other)
      left  = self.in(other)
      right = eq(nil)
      left.or(right)
    end
  end
end

Ransack.configure do |config|
  config.add_predicate 'eq_or_null', arel_predicate: 'eq_or_null'
  config.add_predicate 'not_eq_or_null', arel_predicate: 'not_eq_or_null'
  config.add_predicate 'gteq_or_null', arel_predicate: 'gteq_or_null'
  config.add_predicate 'lt_or_null', arel_predicate: 'lt_or_null'
  config.add_predicate 'in_or_null', arel_predicate: 'in_or_null', wants_array: true
end

agrberg avatar Jan 31 '23 16:01 agrberg