namespace :workers do desc "Populate missing onboarding forms for all workers" task populate_missing_forms: :environment do # Fetch all forms that should be available to workers forms_to_assign = Form.worker # Adjust this based on your criteria for which forms to assign # Iterate over each worker Worker.find_each do |worker| forms_to_assign.each do |form| # Check if the worker already has this form assigned unless worker.onboarding_items.exists?(form: form) # If not, create an OnboardingItem for the worker and this form OnboardingItem.create!(owner: worker, form: form) end end end puts "Missing onboarding forms have been successfully populated for all workers." end end