You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

29 lines
639 B

  1. class Category < ApplicationRecord
  2. has_many :documents
  3. has_many :subcategories, class_name: 'Category', foreign_key: 'parent_id', dependent: :destroy
  4. belongs_to :parent, class_name: 'Category', optional: true
  5. belongs_to :user
  6. before_destroy :check_for_subcategories
  7. def get_fqcn
  8. fqcn = name
  9. cat_parent = parent
  10. while cat_parent != nil do
  11. fqcn = cat_parent.name + "/" + fqcn
  12. cat_parent = cat_parent.parent
  13. end
  14. fqcn
  15. end
  16. private
  17. def check_for_subcategories
  18. unless subcategories.empty?
  19. errors.add(:base, 'Subcategories present')
  20. throw :abort
  21. end
  22. end
  23. end