# frozen_string_literal: true class StoryPage < ApplicationRecord belongs_to :character, optional: true validates :content, length: { maximum: 5000 } validates :position, numericality: { only_integer: true, greater_than_or_equal_to: 0 } default_scope { order(:position, :created_at) } before_create :assign_position private def assign_position self.position = (StoryPage.unscoped.maximum(:position) || -1) + 1 if position.zero? end end