【问题标题】:RailsAdmin how to limit listing records by conditions?RailsAdmin 如何按条件限制列表记录?
【发布时间】:2016-03-28 04:06:00
【问题描述】:

我有两个模型 - UserArticle,其中一个用户可以有多个 articles。 在 User 中有一个列 status,其值为 1 或 0。 我的问题是,在用户列表中,如何通过某些条件限制记录。比如'SELECT * FROM users WHERE status = 1'?

我已经研究了关联范围,但认为这不是我需要的。

例如在下图中,作者“Kame Man”不应出现在列表中。

【问题讨论】:

标签: ruby-on-rails ruby rails-admin


【解决方案1】:

您需要在User 模型中设置scopes,如下所示:

class User < ActiveRecord::Base
  scope :active, -> { where(state: 1) }
  scope :inactive, -> { where(state: 0) }
end

然后配置RailsAdmin 以使用适当的范围作为默认值。在您的情况下,active 如下:

RailsAdmin.config do |config|
  config.model User do
    list do
      scopes [:active]
    end
  end
end

请参阅RailsAdmin Wiki 了解更多信息。

【讨论】:

    【解决方案2】:

    您提供的图片中的“添加过滤器”下拉菜单包含您可以添加条件的列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2023-03-09
      • 2015-08-14
      • 1970-01-01
      • 2021-12-22
      • 2018-05-23
      • 1970-01-01
      相关资源
      最近更新 更多