module Ecom module Core class MaterialItem < ApplicationRecord before_save :assign_serial_number belongs_to :material_identity validates :serial_number, presence: true, uniqueness: true validates :material_identity_id, :material_identity, presence: true validates :serial_number, format: { with: /^SN_\d+$/, message: 'Invalid serial number format', multiline: true } validates :price, numericality: { greater_than: 0 }, allow_nil: true def assign_serial_number self.serial_number = "SN_#{(Time.now.to_f * 1000).to_i}" end end end end