如何在 Rails Admin 中显示无作用域模型? [英] How do I show unscoped models in Rails Admin?

查看:40
本文介绍了如何在 Rails Admin 中显示无作用域模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己需要这个,所以这里是 QA 风格:

I needed this myself, so here it is QA-style:

默认情况下,Rails Admin 显示模型的 default_scope.我如何让它显示每个模型完全没有范围?

By default, Rails Admin shows a model's default_scope. How do I get it to show every model completely unscoped?

推荐答案

方法一

如果您只需要列出记录,您可以使用 scopes 方法来控制返回哪些记录.第一个数组元素是默认值,因此如果将以下内容添加到初始化程序中:

If you only need to list the records you can use the scopes method to control which records are returned. The first array element is the default, so if you add the following to your initialiser:

list do
  scopes [:unscoped]
end

您将看到所有记录.

方法 2

如果你想做的不仅仅是列出模型,你可以创建一个虚拟的 rails 管理模型.例如,假设您有一个带有布尔归档标志的 Post 模型:

If you want to do more than list models you can create a dummy rails admin model. For example, assuming you have a Post model with a boolean archived flag:

class Post < ActiveRecord::Base
  default_scope { archived: false }
end

您可以像这样创建一个虚拟模型以在 rails_admin 中使用(在 app/models/rails_admin 中)

You can create a dummy model to use in rails_admin like so (in app/models/rails_admin)

class RailsAdmin::Post < ActiveRecord::Base
  self.table_name = "posts"
end

然后您将 rails_admin 配置为使用 RailsAdmin::Post 并且所有的 Post 都将没有作用域.

You then configure rails_admin to use RailsAdmin::Post and all of the Posts will be unscoped.

这篇关于如何在 Rails Admin 中显示无作用域模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆