does not work with null values using *_in matchers
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..?
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:
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