ruby-on-rails-3activeadminformtasticmeta-search

How to filter IS NULL in ActiveAdmin?


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 #


Solution

  • Not found a good solution but here is a how. filters of Active_admin are accomplished by meta_search, you can override the functions automatically generated by meta_search in your model to get the behavior that you want, the best way is to use the scope since you need to return a relation in order to chain with other query/scopes, as stated here

    in your model:

    for :as=>:select filters, acitve_admin use the _eq wheres, here is the source code

    scope :map_eq, 
            lambda{ |id|
            if(id !='none')
                where( :map_id=> id)
            else
                where( :map_id=> nil)
            end
            }
    
    #re-define the search method:
    search_method :map_eq, :type => :integer
    

    in your ative_admin register block:

    filter :map_id, :label => 'Assigned', :as => :select, :collection => [['none', 'none'], ['one', 1],['tow', 2]]
    
    # not using :none=>nil because active_admin will igore your nil value so your self-defined scope will never get chained.
    

    Hope this help.