在rails中删除父对象时,如何删除子对象?

时间:2022-09-07 09:16:27

model a:

模式一:

has_many :b, :dependent => :delete_all

model b:

模型2:

belongs_to :a
belongs_to :c

model c:

模型c:

has_many :b

When I delete an a, I would also like to have children b's deleted so that they get removed from any c's that may reference them. However, the above isn't working. I'd appreciate any help.

当我删除一个a时,我也希望将子b删除,这样它们就可以从任何可能引用它们的c中删除。然而,上述方法并不奏效。我感谢任何帮助。

1 个解决方案

#1


35  

Like so:

像这样:

class Widgets < ActiveRecord::Base
  has_many :whatevers, :dependent => :destroy
end

Update

更新

Your recent comment indicates you are using the delete() method to delete your objects. This will not use the callbacks. Please read the manual for specifics.

最近的注释表明您正在使用delete()方法来删除对象。这将不使用回调。详情请参阅手册。

#1


35  

Like so:

像这样:

class Widgets < ActiveRecord::Base
  has_many :whatevers, :dependent => :destroy
end

Update

更新

Your recent comment indicates you are using the delete() method to delete your objects. This will not use the callbacks. Please read the manual for specifics.

最近的注释表明您正在使用delete()方法来删除对象。这将不使用回调。详情请参阅手册。