2012-11-19 33 views
0

我有簡單的主動管理過濾器在我Parent資源:活動管理員「和」與複選框過濾

​​

我想那些或不符合兒童的資源(has_and_belongs_to_many)相關聯的所有家長的資源。工作不壞,但是當我在第一個篩選器中選擇兩個子項時,活動管理器將返回與第一個或第二個子項關聯的所有父資源。我需要「AND」運算符(:child_id和:child_id_not)。

任何解決方法?

回答

3

您將不得不推出自己的範圍。 ActiveAdmin使用meta_search作爲其篩選器(https://github.com/ernie/meta_search)。

在您的管理文件:

filter :child_id_all, 
    :as => :check_boxes, 
    :collection => proc { Child.all } 

filter :child_id_all_not, 
    :as => :check_boxes, 
    :collection => proc { Child.all } 

在你父模型:

scope :child_id_all_in, -> ids { 
    ids.reduce(scoped) do |scope, id| 
    subquery = Parent.select('`parents`.`id`'). 
     joins(:childs). 
     where('`childs`.`id` = ?', id) 
    scope.where("`parents`.`id` IN (#{subquery.to_sql})") 
    end 
} 
scope :child_id_all_not_in, -> ids { 
    ids.reduce(scoped) do |scope, id| 
    subquery = Parent.select('`parents`.`id`'). 
     joins(:childs). 
     where('`childs`.`id` = ?', id) 
    scope.where("`parents`.`id` NOT IN (#{subquery.to_sql})") 
    end 
} 
search_methods :child_id_all_in 
search_methods :child_id_all_not_in