0

我有班级项目,

class Project < ActiveRecord::Base
  has_many :Users
  acts_as_tree
end

我想删除所有子项,如果我删除父项,在这种情况下意味着,如果我删除项目(父项)必须删除所有用户(子项)和子项目(子项)。

我可以按用户添加,dependent: :delete_all但我不知道如何使用acts_as_tree

谢谢

4

2 回答 2

0

act_as_tree如您在源代码(https://github.com/amerine/acts_as_tree/blob/master/lib/acts_as_tree.rb)中看到的,默认行为将children在您销毁时自动销毁parent

但是,您可以以与使用该方法完全相同的方式更改此行为has_many,例如:

act_as_tree dependent: :delete_all

所以在你的情况下,你只需要级联销毁用户:

class Project < ActiveRecord::Base
  has_many :users, dependent: :destroy
  acts_as_tree # dependent: :destroy
end

因此,考虑到您提出问题的方式,@user3506853 的答案实际上是正确的。

干杯!

于 2016-12-07T14:50:25.180 回答
0

尝试使用dependent: :destroy like:-

has_many :users, dependent: :destroy
于 2015-11-30T11:37:44.600 回答