July 1, 2020

How to customize the display name of dynamic ActiveAdmin search filters

Working with ActiveAdmin

At Rootstrap, my first assignment was to develop a Ruby on Rails application which uses ActiveAdmin (with ActiveAdmin Addons) as a framework to implement the user interface.

I had never used ActiveAdmin before and I found myself reading a lot of documentation of the gem, in some cases, the documentation was not enough. For example, when I tried to display a custom name on an AJAX's search select filter at first it was a little tricky. So today I will show you how to do it, in the end, it's really simple!

Showing our custom display_name

In this tutorial, you will learn how to customize the [.c-inline-code]display_name[.c-inline-code] of a [.c-inline-code]search_select[.c-inline-code] the filter provided by ActiveAdmin Addons.

Before starting we have to include in our [.c-inline-code]Gemfile[.c-inline-code] the [.c-inline-code]activeadmin[.c-inline-code] gem and [.c-inline-code]activeadmin-addons[.c-inline-code] gem.

In this example we are going to use a search select to filter people and search a person, our [.c-inline-code]person[.c-inline-code] model has two fields named [.c-inline-code]first_name[.c-inline-code] and [.c-inline-code]last_name[.c-inline-code], and we want to display the full name of the person when we are searching it. The [.c-inline-code]full_name[.c-inline-code] is the concatenation of [.c-inline-code]first_name[.c-inline-code] and [.c-inline-code]last_name[.c-inline-code].

Steps to show our custom display_name

Create a Search Select

First of all, you should create a search select input in ActiveAdmin. This can be used in many places but in this example, we are going to use it in a filter. To do that, go to an ActiveAdmin view and put this code on it:

      filter :person_id,
             as: :search_select_filter,
             fields: %i[first_name last_name],
             display_name: :full_name

(If you are going to use this on a form you have to change [.c-inline-code]search_select_filter[.c-inline-code] to [.c-inline-code]search_select[.c-inline-code]).

The fields array indicates the fields that are used to filter, our input will be matched with the [.c-inline-code]first_name[.c-inline-code] of a person or their [.c-inline-code]last_name[.c-inline-code].

The [.c-inline-code]display_name[.c-inline-code] is the field that our filter is showing to us meanwhile we search.

Setting up the returned JSON

The search select input is requesting a JSON meanwhile the user is searching for a person, for example in this case the filter will be doing requests to this endpoint [.c-inline-code]/admin/people[.c-inline-code] and retrieving a JSON showing all model attributes from a person. This is auto-generated by ActiveAdmin, but we can overwrite the response.

In this application we are using JBuilder to render JSON response so we have to create a file in:


In our example, the view created is in


In this file we can write the custom JSON that we want to retrieve for example:

json.array!(@people) do |person|
  json.extract! person, :id, :full_name

Attention: [.c-inline-code]full_name[.c-inline-code] is a method on [.c-inline-code]person[.c-inline-code] model, we have to write that method in the model and not in a decorator because if we are using this [.c-inline-code]search_select[.c-inline-code] in edit/new view an error will raise when validation fails, ActiveAdmin will call a method from [.c-inline-code]person[.c-inline-code] model named [.c-inline-code]full_name[.c-inline-code] and not from the decorator.

Writing the full_name method

Just write the method that we want in [.c-inline-code]app/models/person.rb[.c-inline-code]:

def full_name
    [first_name, last_name].compact.join(' ')

And now our search select input is ready to display the person full name!

What we learned about Search Select Inputs?

In this article, we learned how to display a custom field in our search select inputs. This will help us to give better search feedback to the user and it can be used in many different scenarios. It also helps us take control of the JSON returned by ActiveAdmin to the input (by default, all attributes are returned and maybe that's not good for us).


Select2_Search ActiveAdmin