Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Admin Rails4 custom filter with Ransack

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?

like image 664
Pioz Avatar asked Oct 20 '22 18:10

Pioz


1 Answers

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

like image 135
seanlinsley Avatar answered Oct 27 '22 10:10

seanlinsley