class Form < ApplicationRecord # Assuming each form can have many onboarding items associated with different owners. has_many :onboarding_items has_many :forms_roles has_many :roles, through: :forms_roles accepts_nested_attributes_for :forms_roles, allow_destroy: true validates :name, presence: true validates :role, inclusion: { in: %w[Participant Worker Vendor Employer] } # Include more validations as necessary after_save :update_onboarding_items private def update_onboarding_items roles.each do |role| # Assuming `role.name` is 'Participant', 'Worker', 'Vendor', or 'Employer' owner_class = role.name.constantize owner_class.find_each do |owner| OnboardingItem.find_or_create_by(owner: owner, form: self) end end end end