class PostVersion < ApplicationRecord before_update do raise ActiveRecord::ReadOnlyRecord, '版は更新できません.' end before_destroy do raise ActiveRecord::ReadOnlyRecord, '版は削除できません.' end belongs_to :post belongs_to :parent, class_name: 'Post', optional: true belongs_to :created_by_user, class_name: 'User', optional: true enum :event_type, create: 'create', update: 'update', discard: 'discard', restore: 'restore' validates :version_no, presence: true, numerically: { only_integer: true, greater_than: 0 } validates :event_type, presence: true, inclusion: { in: event_types.keys } validates :url, presence: true validate :validate_original_created_range scope :chronological, -> { order(:version_no, :id) } private def validate_original_created_range f = original_created_from b = original_created_before return if f.blank? || b.blank? if f >= b errors.add :original_created_before, 'オリジナルの作成日時の順番がをかしぃです.' end end end