Rails活动管理过滤器deleted_at

时间:2015-11-24 14:14:27

标签: ruby-on-rails activeadmin

我希望能够过滤活动管理员中所有软删除的用户。我尝试过以下代码,但它似乎没有按预期工作?

filter :deleted_at, :as => :boolean, :collection => {:Yes => nil, :No => ''}

1 个答案:

答案 0 :(得分:2)

它不起作用,因为它不是正确的方法:

filter :deleted_at, :as => :boolean, :collection => {:Yes => nil, :No => ''}

as: :boolean不是AA命令。

试试这个:

filter :deleted_at, as: :select, collection: [['Yes', nil], ['No', '']]

值是根据你的!如果数据库包含真正的布尔字段,则更改它:

filter :deleted_at, as: :select, collection: [['Yes', true], ['No', false]]

您也可以使用范围来实现它:

scope :deleted, default: true
scope :not_deleted

为您的模型添加这些范围:

scope :deleted, -> { where('deleted_at is not null') }
scope :not_deleted, -> { where(deleted_at: nil) }