class Plan < ApplicationRecord belongs_to :employer_setup_process has_many :plan_benefits, dependent: :destroy accepts_nested_attributes_for :plan_benefits, allow_destroy: true, reject_if: :all_blank after_initialize :create_default_benefits, if: :new_record? private def create_default_benefits benefits = IdCardBenefitsTemplate.find_by(title: "BLANK").id_card_benefits.sort_by(&:sequence) benefits.each do |ben| plan_benefits.new(benefit_desc: ben.benefit_desc, sequence: ben.sequence) end end end