Rails: ActiveAdmin custom filter returning errors

917 Views Asked by At

I am trying to setup a custom filter with my ActiveAdmin installation and it is returning some errors.

User Model:

class User < AR::Base
   has_many :gpas

   def current_gpa
    return nil if gpas.blank?
    @current_gpa ||= (gpas.where(year: classification).first || gpas.order("updated_at DESC").first)
   end

end

ActiveAdmin:

ActiveAdmin.register Athlete do
  filter :current_gpa_value, as: :string
end

The error I get is: ActionView::Template::Error (undefined method current_gpa_value_contains for #<MetaSearch::Searches::User:0x007f982df8fd28>)

2

There are 2 best solutions below

0
David On

ActiveAdmin uses metasearch for filters. Check out this example of how to set up a custom search method.

0
dr. Neox On

You could use scopes:

class User < AR::Base
   has_many :gpas
scope :current_gpa, where(....)



ActiveAdmin.register Athlete do
  scope :current_gpa
end