From 39d6a3a507b22e2a2637484e82ad96b1bc6eedd0 Mon Sep 17 00:00:00 2001 From: Kaio Magalhaes Date: Wed, 11 Sep 2024 10:52:42 -0300 Subject: [PATCH] fix participants in project --- app/models/project.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index 2e12c8c..25ea50e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -93,9 +93,18 @@ def should_generate_new_friendly_id? end def participants - users = statement_of_works.active_in_period(Time.zone.today, - Time.zone.today).map(&:requirements) - .flatten.map(&:assignments).flatten.map(&:user).flatten - User.where(id: users.map(&:id)).distinct + date = Time.zone.today + + # Get the active statement of works in the given period + active_sows = statement_of_works.active_in_period(date, date) + + # Find all the related requirements that are active in the period while keeping it as an ActiveRecord relation + active_requirements = Requirement.where(statement_of_work_id: active_sows.pluck(:id)) + .active_in_period(date, date) + + # Get the users related to assignments from the active requirements + User.joins(assignments: :requirement) + .where(assignments: { requirement_id: active_requirements.pluck(:id) }) + .distinct end end