class Category < ApplicationRecord has_many :documents has_many :subcategories, class_name: 'Category', foreign_key: 'parent_id', dependent: :destroy belongs_to :parent, class_name: 'Category', optional: true belongs_to :user before_destroy :check_for_subcategories def get_fqcn fqcn = name cat_parent = parent while cat_parent != nil do fqcn = cat_parent.name + "/" + fqcn cat_parent = cat_parent.parent end fqcn end private def check_for_subcategories unless subcategories.empty? errors.add(:base, 'Subcategories present') throw :abort end end end