has_and_belongs_to_many destroy only if no reference code example
Example 1: activerecord through
class Doctor < ApplicationRecord
has_many :patients, through: :consultation
end
Example 2: active record dependent destroy
has_many :books, dependent: :destroy