How to add custom filter to Active Admin? How to add custom filter to Active Admin? ruby-on-rails ruby-on-rails

How to add custom filter to Active Admin?


Active admin uses metasearch. For example you can do this:

filter :"subscription_billing_plan_name" , :as => :select, :collection => BillingPlan.all.map(&:name)


Active Admin uses the meta_search gem for its filters. ORed conditions syntax allows to combine several fields in one query, for example

Promo.metasearch(:name_or_address_contains => 'brooklyn')

In Active Admin DSL this translates to

ActiveAdmin.register Promo do  filter :name_or_address, :as => :stringend


To use a custom filter, you can create a scope function and add it as search_methods in the model.

For example, on my User model:

search_methods :role_eqscope :role_eq, -> (role) { where("? LIKE ANY(roles)", role) }

Then in users.rb, I can use my scope as a custom filter:

filter :role, label: "Roles", as: :select, collection: %w[ student teacher parent ]