How to override default_scope in ActiveAdmin in Rails
Asked Answered
F

5

15

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?

Fondafondant answered 27/2, 2012 at 20:41 Comment(0)
L
47
ActiveAdmin.register Post do
  controller do
    def scoped_collection
      Post.unscoped
    end
  end
end 
Laquitalar answered 3/4, 2012 at 17:35 Comment(2)
this isn't working for me, it appears to still be scoped.Folketing
turns out I had scoped_collection defined again further down the file.Folketing
C
7
scope('all', default: true) { |scope| scope.where(...) }
Cinematography answered 27/12, 2017 at 10:14 Comment(0)
H
1

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
Hendecahedron answered 8/10, 2013 at 11:17 Comment(0)
E
0
  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/

Equalitarian answered 19/9, 2012 at 0:22 Comment(0)
H
-1

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
Hermaphrodite answered 27/2, 2012 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.