ぼざクリタグ広場 https://hub.nizika.monster
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.
 
 
 
 
 
 

32 lines
867 B

  1. class Material < ApplicationRecord
  2. include MyDiscard
  3. default_scope -> { kept }
  4. belongs_to :parent, class_name: 'Material', optional: true
  5. has_many :children, class_name: 'Material', foreign_key: :parent_id, dependent: :nullify
  6. belongs_to :tag, optional: true
  7. belongs_to :created_by_user, class_name: 'User', optional: true
  8. belongs_to :updated_by_user, class_name: 'User', optional: true
  9. has_one_attached :file, dependent: :purge
  10. validate :file_must_be_attached
  11. validate :tag_must_be_material_category
  12. private
  13. def file_must_be_attached
  14. return if url.present? || file.attached?
  15. errors.add(:url, 'URL かファイルのどちらかは必須です.')
  16. end
  17. def tag_must_be_material_category
  18. return if tag.blank? || tag.material?
  19. errors.add(:tag, '素材カテゴリのタグを指定してください.')
  20. end
  21. end