This commit is contained in:
2026-03-29 21:27:59 +09:00
parent 2adff3966a
commit c28326b941
10 changed files with 274 additions and 30 deletions
+31
View File
@@ -0,0 +1,31 @@
class Material < ApplicationRecord
include MyDiscard
default_scope -> { kept }
belongs_to :parent, class_name: 'Material', optional: true
has_many :children, class_name: 'Material', foreign_key: :parent_id, dependent: :nullify
belongs_to :tag, optional: true
belongs_to :created_by_user, class_name: 'User', optional: true
belongs_to :updated_by_user, class_name: 'User', optional: true
has_one_attached :file, dependent: :purge
validate :file_must_be_attached
validate :tag_must_be_material_category
private
def file_must_be_attached
return if url.present? || file.attached?
errors.add(:url, 'URL かファイルのどちらかは必須です.')
end
def tag_must_be_material_category
return if tag.blank? || tag.material?
errors.add(:tag, '素材カテゴリのタグを指定してください.')
end
end