【问题标题】:How to override default_scope in ActiveAdmin in Rails如何在 Rails 的 ActiveAdmin 中覆盖 default_scope
【发布时间】:2012-02-27 20:41:20
【问题描述】:

在使用 ActiveAdmin 注册的资源中,我为模型定义了以下 default_scope:

default_scope :order => 'activities.updated_at DESC'

这显然使我无法通过单击列标题来更改资源索引页上的排序。有没有办法保持这个默认范围但让 Active Admin 排序工作?

【问题讨论】:

    标签: ruby-on-rails activeadmin default-scope


    【解决方案1】:
    ActiveAdmin.register Post do
      controller do
        def scoped_collection
          Post.unscoped
        end
      end
    end 
    

    【讨论】:

    • 这对我不起作用,它似乎仍在范围内。
    • 原来我在文件的下方再次定义了 scoped_collection。
    【解决方案2】:
    scope('all', default: true) { |scope| scope.where(...) }
    

    【讨论】:

      【解决方案3】:

      试试这个解决方案。

      #/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
      

      【讨论】:

        【解决方案4】:
          scope_to do
           Class.new do
            def self.cookies
             Cookie.unscoped
            end
           end
          end
        

        更多:http://blogs.burnsidedigital.com/2012/09/ignoring-default_scope-in-activeadmin/

        【讨论】:

          【解决方案5】:

          您是在尝试确定活动的范围还是仅对它们进行排序,因为此调用仅对它们进行排序,实际上并没有在最严格的概念中确定查询的范围。

          根据我对ActiveAdmin 的了解以及他们的文档所述,您可能应该这样设置。

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

          【讨论】:

            猜你喜欢
            • 2010-12-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-06-11
            • 1970-01-01
            • 2022-10-04
            相关资源
            最近更新 更多