如何让 ActiveAdmin 为索引加载特定数据
How To Make ActiveAdmin Load Particular Data For Index
这就是我想要的。我已经 table 调用了一些 x
,我在活动管理页面中添加了这个 table。添加后发生的事情是它加载索引页面 table 中的所有数据。但我只想加载在某些情况下为真的数据。
例如 table x
有一列名为 val
,table 中的值为 val(1,2,3,4,5,6,7,8,9,10)
。我只想加载大于 5 的值。如何在 Active Admin 中执行此操作?
您可以覆盖 scoped_collection
方法。
为此,在 AR 模型中创建一个范围:
scope :greater_then_five, -> { where("some_attribute >= ?", 5) } # any condition you need
然后简单地在 AA 中使用这个范围:
controller do
def scoped_collection
MyModel.greater_then_five
end
end
这样您将从数据库中获取数据并由 AA 过滤使用。
这就是我想要的。我已经 table 调用了一些 x
,我在活动管理页面中添加了这个 table。添加后发生的事情是它加载索引页面 table 中的所有数据。但我只想加载在某些情况下为真的数据。
例如 table x
有一列名为 val
,table 中的值为 val(1,2,3,4,5,6,7,8,9,10)
。我只想加载大于 5 的值。如何在 Active Admin 中执行此操作?
您可以覆盖 scoped_collection
方法。
为此,在 AR 模型中创建一个范围:
scope :greater_then_five, -> { where("some_attribute >= ?", 5) } # any condition you need
然后简单地在 AA 中使用这个范围:
controller do
def scoped_collection
MyModel.greater_then_five
end
end
这样您将从数据库中获取数据并由 AA 过滤使用。