How to filter IS NULL in ActiveAdmin?

Viewed 9471

I've a table with an integer column called "map_id", I want to add an activeadmin filter to filter if this column IS NULL or IS NOT NULL.

How could this be implemented ?

I tried the following filter

filter :map_id, :label => 'Assigned', :as => :select, :collection => {:true => nil, :false => ''}

But, I get the following error message :

undefined method `map_eq' for #

5 Answers

With ransackable scopes:

On the ActiveAdmin resource definition:

filter :map_id, :label => 'Assigned', as: :select, :collection => [['Is Null', 'none'], ['Not null', 'present']]

On your model:

scope :by_map_id, ->(id) { (id == 'none' ? where(map_id: nil) : where('map_id IS NOT NULL')) }

def self.ransackable_scopes(_auth_object = nil)
  %i[by_map_id]
end
Related