module Ecom module Core class Payment < ApplicationRecord belongs_to :payroll has_many :payment_details, class_name: 'Ecom::Core::PaymentDetail', dependent: :destroy validates :from, :to, presence: true before_validation :set_payment_order def set_payment_order unless payroll self.payment_order = 1 return end previous_payment = payroll.payments.find_by(approved: true) self.payment_order = previous_payment ? previous_payment.payment_order + 1 : 1 end end end end