ActiveAdmin - Using scopes with filters

家住魔仙堡 提交于 2019-12-20 23:24:07

问题


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

[update]

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

回答1:


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.




回答2:


By default, ActiveAdmin wants scopes to just provide a symbolized method name. When you define scopes this way, they can be chained to the scoping already provided by filters and they work together seamlessly.

So your mistake is explicitly calling Model#class_method instead of providing :symbolized_class_method_name (with an implied owner of the current model).

Filters and scopes will work together if you replace this code:

scope :all do |example|
  Example.with_deleted
end

scope :deleted do |example|
  Example.only_deleted
end

With this:

scope "Deleted", :only_deleted
scope "All", :with_deleted


来源:https://stackoverflow.com/questions/10523407/activeadmin-using-scopes-with-filters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!