I have created project, stage, task and sub_task scaffolds:
projecthas a one-to-many association withstagestagehas a one-to-many association withtasktaskhas one-to-many association withsub_task.
stage, task, and sub_task tables have all a field planned_end_date and status.
Now I want to print the total stages, tasks, sub_tasks that aren't completed until planned_end_date for each project in the projects#index action.
How can I do that in a Rails model?
class Project < ApplicationRecord
has_many :stages, dependent: :destroy
validate :end_after_start
private
def end_after_start
return if to_date.blank? || form_date.blank?
if to_date < form_date
errors.add(:to_date, "Project end date must be same or after the start date")
end
end
end
what i have tried-
project#index.html.erb
<% @projects.each do |project| %>
<tr>
<td><%= project.project_name %></td>
<% @stages = Stage.where(project_id: @projects.ids) %>
<% @tasks = Task.where(stage_id: @stages.ids) %>
<% @sub_tasks = SubTask.where(task_id: @tasks.ids) %>
<% stage_counter = 0 %>
<% task_counter = 0 %>
<% sub_task_counter = 0 %>
<% @stages.each{|s| stage_counter += 1 if s.planned_end_date.past? && s.status == 0 || s.planned_end_date.past? && s.status == 2} %>
<% @tasks.each{|s| task_counter += 1 if s.planned_end_date.past? && s.status == 0 || s.planned_end_date.past? && s.status == 2} %>
<% @sub_tasks.each{|s| sub_task_counter += 1 if s.planned_end_date.past? && s.status == 0 || s.planned_end_date.past? && s.status == 2} %>
<% @count =0 %>
<% @count = stage_counter + task_counter + sub_task_counter %>
<td><span class="alert"><%= @count.to_s + " Activity Pending" %></span></td>
what is code does is prints total number of pending stages, tasks and sub_tasks for all projects and prints same count for every project. I want to print pending stages+tasks+sub_tasks for every project's total pending stages+tasks+sub_tasks. what