Reputation: 582
I am querying my model like so:
MyModel.search(year_not_in: [2000, 2010, 2015])
But am getting back results from MyModel
ONLY where year
is not nil
. For example, the query hits where MyModel.year = 1999
but not where MyModel.year = nil
. I expect nil
to not be in that array but perhaps that is how it's defined in SQL.
Is there a way I can override the year
or year_not_in
ransacker to add the condition above?
Upvotes: 3
Views: 755
Reputation: 1285
First of all : this solution test with ransack (2.4.2)
and rails 6.1.3
To achieve this, GEM ransack give us a solution is to add a new predicate :
If you'd like to add your own custom Ransack predicates
not_in_or_null
You can do it like this way :
"config/initializers/ransack.rb"
You add this
Ransack.configure do |config|
config.add_predicate 'not_in_or_null', arel_predicate: 'not_in_or_null'
end
module Arel
module Predications
def not_in_or_null(other)
left = not_in(other)
right = eq(nil)
left.or(right)
end
end
end
And than, you have a new predicate now.
Upvotes: 0
Reputation: 582
I was able to solve this without overriding, though I would still be interested in how to do that. Joining statements like so was my solution:
.search(m: 'or', year_not_in: [2000, 2010, 2015], year_null: true)
Upvotes: 3