I figured out a solution, but it’s not very.
The good news is that Ransack has no problem finding multiple terms. The search uses the predicate cont_all . The next line works to find emails containing "blacksmith" and ".edu".
User.ransack(email_cont_all: ['smith','.edu'] ).result
Since these searches in Ransack are easy, they are probably straightforward in Activeadmin, right? Wrong! To make them work, I had to do three things.
I put the special ransack (aka ransacker ) method in User.rb I called ransacker email_multiple_terms .
class User < ActiveRecord::Base
I declared a filter in my activeadmin control panel and linked it to ransacker. Note that the search predicate cont_all appended to the name ransacker.
admin/User.rb :
ActiveAdmin.register User do # ... filter :email_multiple_terms_cont_all, label: "Email", as: :string
This line creates a filter widget in Activeadmin. We are almost there. One problem remains: Activeadmin sends search queries to a regular string (for example, "smith .edu" ), while our ransacker wants the search terms to be an array. Somewhere we need to convert a single string into an array of search terms.
I modified activeadmin to break the search string under certain conditions. The logic is in the method that I added to lib / active_admin / resource_controller / data_access.rb.
def split_search_params(params) params.keys.each do |key| if key.ends_with? "_any" or key.ends_with? "_all" params[key] = params[key].split
Then I called this method inside apply_filtering .
def apply_filtering(chain) @search = chain.ransack split_search_params clean_search_params params[:q] @search.result end
This code is in my own activeadmin plugin, here: https://github.com/dH-/activeadmin
So, to get it working with multiple terms, follow steps 1 and 2 above and turn on my AA plug in your Gemfile:
gem 'activeadmin', :git => 'git://github.com/dH-/activeadmin.git'
NTN.
If anyone got a simpler method, please share!