in Ruby on Rails 4, let's say a parent has many children. When a parent is removed, children must also be removed. In addition, the child should not be removed unless he is an orphan. How to do it?
I tried the following
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
However, before_destroy checks nothing is deleted. Is there a way to report this method if the cause of the call is parental deletion?
Is this a strange thing to ask for? I mean, preventing the removal of child elements.
ruby-on-rails-4 has-and-belongs-to-many
user2553863
source share