在Ruby on Rails 4中,假设父母有很多孩子.删除父项后,还必须删除子项.除此之外,除非儿童是孤儿,否则不得删除儿童.怎么做?
我尝试了以下内容
class Parent < ActiveRecord::Base
has_many :children, inverse_of: :parent, dependent: :destroy
end
class Child < ActiveRecord::Base
belongs_to :parent, inverse_of: :children
before_destroy :checks
private
def checks
not parent # true if orphan
end
end
Run Code Online (Sandbox Code Playgroud)
但是,使用before_destroy检查,不会删除任何内容.如果被调用的原因是因为父删除,有没有办法告诉这个方法?
要求这是一件奇怪的事吗?我的意思是,防止删除孩子.