Skip to content

Instantly share code, notes, and snippets.

@amitsaxena
Last active October 24, 2019 11:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amitsaxena/f70768b0bb935278a0900607b85639cb to your computer and use it in GitHub Desktop.
Save amitsaxena/f70768b0bb935278a0900607b85639cb to your computer and use it in GitHub Desktop.
How to write an Active Admin custom filter (custom search)
ActiveAdmin.register MyModel do
filter :custom_field, :as => :string, :label => "Custom Field Display Name"
end
class MyModel < ActiveRecord::Base
search_methods :custom_field_search
def self.custom_field_search(name)
# This method should return an ActiveRecord::Relation Object with MyModel objects
# Put in your complex search logic here
objects = Model2.where(:user_type => "Student").where('_name like ?', "%#{name}%")
MyModel.where(:id => objects.map{|u| u.user_id})
end
end
@davidwparker
Copy link

davidwparker commented Aug 7, 2017

undefined method `search_methods'

Currently AA uses ransacker, so this is old.

@acnjr2010
Copy link

undefined method `search_methods'

@gsum
Copy link

gsum commented Mar 25, 2019

undefined method search_methods' for #Class:0x00007ffb484fa5d8
Did you mean? instance_methods`

@amitsaxena
Copy link
Author

I guess this was for an older version of active_admin. I don't use it any longer to be able to debug and fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment