2013-12-17 30 views
5

我有資源訂單。在我的管理面板(activeadmin)中,我需要一個布爾過濾器來獲取用戶具有特定角色的所有訂單。Active Admin Rails4自定義篩選器與Ransack

在Order類我有這樣一個範圍:

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

在主動管理order.rb我已經添加後續過濾:

filter :client_only, as: :check_boxes 

使用Rails 3(元搜索)我可以添加

search_method :client_only_in, type: :boolean 

得到過濾器,但與軌道4和Ransack我不知道如何做到這一點。

不添加search_method到命令模式出現錯誤

undefined method `client_only_in' for Ransack::Search 

當我訪問索引管理訂單頁面。

任何幫助?

回答

3

我不知道的方式做到這究竟,但你總是可以有一個選擇字段來篩選基於用戶角色:

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

UPDATE:嗯,如果一個訂單belongs_to用戶比它應該是:user_role而不是:users_role

+0

我無法在Rails 4中獲得此項工作。以下是我找到的解決方案:http://cavewall.jaguardesignstudio.com/2014/05/01/activeadmin-filters -with-洗劫/ – smcdrc