ActiveAdmin-将范围与过滤器一起使用 [英] ActiveAdmin - Using scopes with filters

查看:55
本文介绍了ActiveAdmin-将范围与过滤器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ActiveAdmin模型中,我有一个自定义范围来显示已删除的记录,还有几个过滤器用于按特定列搜索记录。

In my ActiveAdmin model I have a custom scope to show deleted records and several filters for searching records by specific columns.

单独使用过滤器或将其组合在一起可以正常使用。

Using the filters individually or combined together works as expected.

使用范围可以按预期运行。

Using a scope works as expected.

问题是,使用范围似乎会覆盖所有过滤器,并且在选择范围后,添加的任何过滤器都无济于事。

The problem is that using a scope seemingly overrides all the filters and after selecting a scope any filter added does nothing.

这里有任何想法吗?我想要的是能够显示特定范围,然后仍然能够在该范围内过滤结果。

Anyone have any ideas here? What I want is to be able to show a specific scope and then still be able to filter results within that scope.

    ActiveAdmin.register Example do
      scope :deleted do |example|
        Example.only_deleted
      end

      scope :all do |example|
        Example.with_deleted
      end

      filter :title
      filter :description

      index do
        column :title
        column :description
      end

    end

[更新]

这是我一直使用的解决方案。我在模型上设置了with_deleted范围,并在侧面包括了用于显示/隐藏已删除结果的过滤器。不理想,因为还会显示最初删除的结果,但是至少所有过滤器都可以一起使用。

Here's the solution I've gone with. I set the with_deleted scope on the model and include filter on the side for showing/hiding deleted results. Not ideal since initially deleted results are also shown, but at least all filters can be used together.

    ActiveAdmin.register Example.with_deleted do

      filter :title
      filter :description
      filter :deleted, :as => :select, :collection => {:true => nil, :false => false }

      index do
        column :title
        column :description
      end

    end


推荐答案

不是作用域,而是创建另一个过滤器,该过滤器将根据是否删除示例或所有示例来选择条件上的记录。并根据需要应用尽可能多的过滤器。

Rather than scope create another filter which will select the records on criterion based on if examples are deleted or all. And apply as many filter as you want.

或者在过滤器中,同时计算要在其上运行过滤器的选择器,并相应地运行范围,然后在该过滤器上放置条件选择器。

Or in filter while calculating the selector on which you will run the filter run the scope accordingly and then put the filter conditions on that selector.

这篇关于ActiveAdmin-将范围与过滤器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆