我一直在寻找一种方法来检测某个对象是否有子对象,或者不知道是否可以限制删除该对象。有很多方法可以添加这种方法。例如
def can_destroy?
self.class.reflect_on_all_associations.all? do |assoc|
assoc.options[:dependent] != :restrict ||
(assoc.macro == :has_one && self.send(assoc.name).nil?) ||
(assoc.macro == :has_many && self.send(assoc.name).empty?)
end
end
但是我想知道现在Rails 5中是否已经内置了这样的东西,因为在我看来,这似乎是许多开发人员所需要的方法。