45

Active Admin allows me to define filters that are displayed on the index page like so:

ActiveAdmin.register Promo do

  filter :name
  filter :address
  filter :city
  filter :state
  filter :zip

end

I would like to combine all the fields above into one, so that I can search for Promos that contain the search string in name or full address. My model already has a named scope that I can use:

class Promo < ActiveRecord::Base
  scope :by_name_or_full_address, lambda { |q| where('name LIKE :q OR address LIKE :q OR city LIKE :q OR state LIKE :q OR zip LIKE :q', :q => "%#{q}%") }
end

8 Answers 8

31

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

filter :"subscription_billing_plan_name" , :as => :select, :collection => BillingPlan.all.map(&:name)
4
  • 8
    note that you probably want your collection to be re-evaluated every time the page loads by putting in a lambda: collection: lambda { BillingPlan.all.map(&:name) }. Otherwise if you add a BillingPlan after your app boots, it won't be in the selection.
    – mkirk
    Oct 3, 2013 at 21:11
  • getting a wrong number of arguments error (1 for 0) if i use a lambda statement, idea why? Using rails 3.2.17 with active_admin 0.6
    – Skully
    Apr 9, 2014 at 10:19
  • Nice for model enum fields
    – MegaTux
    Jun 7, 2017 at 20:54
  • 1
    use .pluck(:name) instead .map to avoid bringing unnecessary data from the database.
    – Gonzalo S
    Sep 13, 2017 at 14:41
29

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 => :string

end
1
  • 1
    Although it uses Ransack it seems to still support this kind of filter. In my testing, you need to explicitly state as: :string. You can also pass label: "Custom Label" if you want a different label.
    – ap.
    Jan 15, 2014 at 18:05
18

Another way of doing such filtering in newer version of active admin:

# app/admin/my_model.rb
filter :my_custom_filter,
as: :numeric,
label: 'Custom Filter',
filters: [:eq]

Then add following 2 functions in your model file

Your filtering logic:

def self.my_custom_filter_eq(value)
  where(column_1: value) # or probably a more complex query that uses the value inputted by the admin user
end

Registering new filter for Ransack

def self.ransackable_scopes(_auth_object = nil)
  %i(my_custom_filter_eq)
end
1
  • In my case I also had to add my_custom_filter to the model's def self.ransackable_attributes method Oct 27, 2023 at 13:50
13

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_eq
scope :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 ]
2
  • 1
    Throws syntax error, unexpected tIDENTIFIER, expecting '}' (SyntaxError)
    – Ain Tohvri
    Aug 18, 2014 at 12:09
  • 1
    Ransack uses ransackers instead of search_methods for customization
    – MegaTux
    Aug 26, 2019 at 20:55
8

Answering in 2018. ActiveAdmin uses Ransack.

On model itself you need to add Ransack formatter:

ransacker :my_custom_filter, formatter: -> (category_id) {
    ids = MyModel.where(category_id: category_id).pluck(:id) # return only id-s of returned items.
    ids.present? ? ids : nil # return ids OR nil!
} do |parent| # not sure why this is needed .. but it is :)
    parent.table[:id]
end 

In ActiveAdmin file you need to specify the rule:

filter :my_custom_filter_in, as: :select, collection: -> { Category.all } # sometimes my_custom_filter_eq - depending on what you want .. Specify different "as" when you need it. 
2
  • hey I tried this, It's working for & we need parent because it's the column in the table that we'll be searching. Jan 18, 2023 at 17:41
  • And I am getting ids as array how to query this for all the array values from ids. Please let me know. Jan 18, 2023 at 17:42
6

I found better way of doing that. You just need to add:

config.clear_sidebar_sections!

sidebar :filters do
  render partial: 'search'
end

And then make form inside the _search partial with the builder ActiveAdmin::FormBuilder as it did in:

https://github.com/gregbell/active_admin/blob/master/lib/active_admin/filters/forms.rb

For more information how to do it, look to this gist:

https://gist.github.com/4240801

Another idea is to create class:

module ActiveAdmin
  module Inputs
    class FilterCustomStringInput < FilterStringInput
      def input_name
        "#{super}"
      end
    end
  end
end

that will be able to invoke by as: :custom_string, but I don't like that idea, because you can find soon, that you will need to create custom_select and so on...

2

I have model WithdrawalRequest which belongs to User model.

For filtering withdrawal requests by user's email need write:

filter :user_id, :as => :select, :collection => User.all.map {|user| [user.email, user.id]}
0

This worked for me:

In my model

  scope :active, -> { where(inactive_at: nil) }
  scope :inactive, -> { where.not(inactive_at: nil) }

  ...

  ransacker :listing_status, formatter: proc{ |status|
    ids = status == 'Active' ? active.ids : inactive.ids
    ids = ids.present? ? ids : nil
  }, splat_params: true do |parent|
    parent.table[:id]
  end

In my admin file

filter :listing_status_in, as: :select, collection: %w(Active Inactive), label: 'Listing Status'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.