ruby-on-railsactiveadmindefault-scope

Skip default scope when you filtering in ActiveAdmin


I have a default scope in ActiveAdmin excluding 'pending' status because we have a lot of this status and we didn't want to see it by default. But when we search by filter, we want to skip this default scope and include 'pending' status. How to do this ?

My model :

class MyModel < ActiveRecord::Base
   validates :status, presence: true,
                      inclusion: { in: %w(pending published accepted declined cancelled) } 
   scope :published, lambda {
      where("bookings.published_at IS NOT NULL")
   }   
end

ActiveAdmin Model :

ActiveAdmin.register MyModel do
  actions :index, :show

  config.sort_order = "locked_at_desc"
  config.scope :published, default: true

  index do
    column :id
    column :status
    actions
  end
end

Solution

  • You need to add :all scope in the admin file. Then you can search by filter which will include pending status under the :all scope. If you need to speedup loading all your records, you may provide show_count: false in :all scope.

    ActiveAdmin.register MyModel do
      ...
      scope :published, default: true
      scope :all # scope :all, show_count: false
      ...
    end