我正在使用ActiveAdmin的ROR应用程序上工作。我想实现这样的目标-当我们删除用户时,由他注册的设备也应该被删除。
批处理操作的代码如下-
batch_action :destroy, :confirm => "Are you sure you want to delete these users?", do |ids|
SearchableUser.where(id: ids).destroy_all
redirect_to users_path, :notice => "Successfully destroyed users"
end
我想在此处以批处理操作添加命令,该命令首先删除该用户注册的设备,然后删除该用户。
注意:想要实现此目的而不影响模型。
有什么办法可以做到这一点?任何形式的帮助将不胜感激。
最好的选择是在模型上指定dependent: :destroy
或dependent: :delete_all
。如果那不是您可以做的,那么理论上您可以通过以下方式实现相同的目的(未经测试):
batch_action :destroy, :confirm => "Are you sure you want to delete these users?", do |ids|
Children.where(parent_id: ids).destroy_all
Parent.where(id: ids).destroy_all
redirect_to users_path, :notice => "Successfully destroyed users"
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句