8

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
xivusr
  • 419
  • 5
  • 15

2 Answers2

4

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
armchairdj
  • 1,030
  • 11
  • 13
  • 3
    Five years later, the ActiveAdmin docs still don't address this. I ran into the very same problem before I figured it out. – armchairdj Mar 06 '18 at 18:24
  • 3
    For the people coming from Google (like me) I would add that the scope works if used correctly. Thus instead of writing ```scope :all do |example| Example.with_deleted end``` you should do ```scope :all do |example| example.with_deleted end``` – Lorenzo Baracchi Feb 18 '19 at 10:38
-3

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.

abhas
  • 5,193
  • 1
  • 32
  • 56