Question

I've the resource Order. In my admin panel (activeadmin) I need a boolean filter to get all orders where the user have a specific role.

In the Order class I've a scope like this:

class Order
  belongs_to :user
  scope :client_only_in, -> { joins(:user).where('users.role = ?', 'client') }
end

In active admin order.rb I've add the follow filter:

filter :client_only, as: :check_boxes

With rails 3 (metasearch) I can add

search_method :client_only_in, type: :boolean

to get the filter, but with rails 4 and Ransack I dont know how to do this.

Without adding the search_method to the order model I get the error

undefined method `client_only_in' for Ransack::Search

when I visit index admin orders page.

Any help?

Was it helpful?

Solution

I don't know of a way to do exactly this, but you can always have a select field to filter based on User roles:

filter :users_role, as: :select, multiple: true, collection: proc{ User.uniq.pluck :role }

UPDATE: Ah, if an Order belongs_to a User than it should be :user_role instead of :users_role

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top