How to search for a substring in a custom filter in ActiveAdmin?

633 views Asked by At

In the past:

A model User has a string filed first_name. If I write filter :first_name in ActiveAdmin model it would display a drop-down besides the input field with the options Contains, Equals, Starts with, Ends with. So for examle if I choose Contains and write ik, it would find, for example Mike.

Now:

If I use a custom filter I just get a large input, spanning across two columns, which only does exact search. I define my filter like so:

  1. Create a scope in the model.
  2. Whitelist it with ransackable_scopes.
  3. Add it to the admin model like so: filter :my_filter, as: :string.

How do I get the functionality to seach for substrings?

1

There are 1 answers

1
Alejandro Babio On

To search for substrings you must use the Ransack Search Predicate Syntax i.e. if you want search a substring on field first_name you must configure this way:

filter :first_name_cont, as: :string, label: "Name"

You can find more information here

I hope it helps.