class ChatMessage < ApplicationRecord belongs_to :chat_room belongs_to :sender, class_name: "Character", optional: true belongs_to :target, class_name: "Character", optional: true validates :body, length: { minimum: 1, maximum: 255 } validate :sender_has_access private def sender_has_access if sender && !sender.user.has_permission_or_higher?(self.chat_room.permission_level) errors.add(:chat, "You don't have permission to send that chat.") end end end