14
votes

In a resource registered with ActiveAdmin, I have the following default_scope defined for the model:

default_scope :order => 'activities.updated_at DESC'

This apparently prevents me from being able to change the sorting on the resource's index page by clicking on the column titles. Is there a way to keep this default scope but get Active Admin sorting to work?

5

5 Answers

45
votes
ActiveAdmin.register Post do
  controller do
    def scoped_collection
      Post.unscoped
    end
  end
end 
7
votes
scope('all', default: true) { |scope| scope.where(...) }
1
votes
  scope_to do
   Class.new do
    def self.cookies
     Cookie.unscoped
    end
   end
  end

more here: http://blogs.burnsidedigital.com/2012/09/ignoring-default_scope-in-activeadmin/

1
votes

Try out this solution.

#/admin/user.rb
controller do
  # for index page
  def active_admin_collection
    User.unscoped { super }
  end

  # for show, edit
  def resource
    User.unscoped { super }
  end
end
-1
votes

Are you trying to scope the activities or just order them, because this call only orders them, it is not actually scoping the query in the strictest idea.

From what I know of ActiveAdmin and from what their documentation states, you should probably set it up like this.

  class Activities < ActiveRecord::Base
    default_scope lambda { where :updated_at => true }
  end