ghe*_*ton 11 ruby activerecord ruby-on-rails
我有这样的关系:
Parent
has_many :children
Child
belongs_to :parent
Run Code Online (Sandbox Code Playgroud)
我想要做的是删除父母,如果没有更多的孩子.所以要做到这一点我有:
Child
before_destroy :destroy_orphaned_parent
def destroy_orphaned_parent
parent.children.each do |c|
return if c != self
end
parent.destroy
end
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我也想将父级的删除级联到子级.我通常会这样做:
Parent
has_many :children, :dependent => :destroy
Run Code Online (Sandbox Code Playgroud)
这会导致WebRick服务器在测试时崩溃.我假设这是由于最后一个孩子的无限循环删除父母删除孩子等.
我开始认为有更好的方法可以做到这一点?有人有主意吗?有没有办法阻止这种递归?
我通过以下方式完成了这项工作:
before_destroy :find_parent
after_destroy :destroy_orphaned_parent
def find_parent
@parent = self.parent
end
def destroy_orphaned_parent
if @parent.children.length == 0
@parent.destroy
end
end
Run Code Online (Sandbox Code Playgroud)
根据Anwar的建议,这也可以使用around
回调来完成,如下所示:
around_destroy :destroy_orphaned_parent
def destroy_orphaned_parent
parent = self.parent
yield # executes a DELETE database statement
if parent.children.length == 0
parent.destroy
end
end
Run Code Online (Sandbox Code Playgroud)
我没有测试过上述解决方案,如有必要,请随时更新.
一些想法:
您可以删除孤立的父母after_destroy
(使用http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/a3f12d578f5a2619上的语句查找
)
您可以在before_destroy
包含父级ID的位置设置一些实例变量,然后在after_destroy
回调中根据此ID进行查找,并根据对子级的计数来决定是否删除父级
归档时间: |
|
查看次数: |
3304 次 |
最近记录: |