How to filter IS NULL in ActiveAdmin?

前端 未结 5 746
迷失自我
迷失自我 2020-12-25 14:14

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 implem

5条回答
  •  礼貌的吻别
    2020-12-25 14:59

    The new version of ActiveAdmin uses Ransacker. I manage to got it working this way:

    On the admin

    filter :non_nil_map_id, :label => 'Assigned', :as => :select, :collection => [['none', 'none'], ['one', 1],['tow', 2]]
    

    For consistency, I took the same code from @Gret answer just changing the filter name

    On your model

    ransacker :not_nil_map_id, :formatter => proc {|id|  map_id != 'none' ? id : 'none' } do |parent|
        parent.table[:id]
    end
    

    This should trigger a search against nil in case the id is 'none', and active record will return all the nil id entries.

    This thread helped a lot.

提交回复
热议问题