Filter “主动管理”;及;带有复选框的过滤器

Filter “主动管理”;及;带有复选框的过滤器,filter,activeadmin,meta-search,Filter,Activeadmin,Meta Search,我的父资源中有一个简单的活动管理筛选器: filter :child_id, :as => :check_boxes, :collection => proc { Child.all } filter :child_id_not, :as => :check_boxes, :collection => proc { Child.all } 我想获取所有与子资源关联或不关联的父资源(has_和_属于_-many)。工作不错,但当我在第一个筛选器中选择两个孩子时,activ

我的
父资源中有一个简单的活动管理筛选器:

filter :child_id, :as => :check_boxes, :collection => proc { Child.all }
filter :child_id_not, :as => :check_boxes, :collection => proc { Child.all }
我想获取所有与子资源关联或不关联的父资源(has_和_属于_-many)。工作不错,但当我在第一个筛选器中选择两个孩子时,active admin将返回与第一个或第二个孩子关联的所有父资源。我需要两个(:child\u id和:child\u id\u not)的“AND”运算符


有什么解决办法吗?

您必须展开自己的范围。ActiveAdmin使用meta_搜索其过滤器(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

你必须展开你自己的范围。ActiveAdmin使用meta_搜索其过滤器(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