【问题标题】:Active admin "AND" filter with check boxes带有复选框的活动管理员“AND”过滤器
【发布时间】:2012-11-19 16:13:00
【问题描述】:

我的Parent 资源中有简单的活动管理员过滤器:

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

我想获取所有与子资源(has_and_belongs_to_many)关联或不关联的父资源。效果不错,但是当我在第一个过滤器活动管理员中选择例如两个孩子时,会返回与第一个或第二个相关联的所有父资源。我需要两个(:child_id和:child_id_not)的“AND”运算符。

有什么解决方法吗?

【问题讨论】:

    标签: filter activeadmin meta-search


    【解决方案1】:

    您将不得不推出自己的范围。 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多