module EducodeSales class MoneyPlan < ApplicationRecord belongs_to :staff belongs_to :follow_up, counter_cache: true enum clazz: ['未完成', '已完成'] after_save :update_return_money after_destroy :update_return_money private def update_return_money if self.clazz == '已完成' follow_up.update(return_money: follow_up.money_plans.where(clazz: '已完成').sum(:amount)) all_follow_ups_ids = follow_up.business.follow_ups.pluck(:id) follow_up.business.update(return_money: EducodeSales::MoneyPlan.where(clazz: '已完成', follow_up_id: all_follow_ups_ids).sum(:amount)) end end end end