Ruby on rails 如何在rails模型中执行计算?

Ruby on rails 如何在rails模型中执行计算?,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我创建了项目、阶段、任务和子任务脚手架: 项目与阶段有一对多的关联 阶段与任务有一对多的关联 任务与子任务有一对多关联。 阶段、任务和子任务表都有一个字段“计划结束日期”和“状态” 现在我想打印projectsindex操作中每个项目在计划结束日期之前未完成的总阶段、任务和子任务 在Rails模型中如何实现这一点 class Project < ApplicationRecord has_many :stages, dependent: :destroy validate :end

我创建了项目、阶段、任务和子任务脚手架:

项目与阶段有一对多的关联 阶段与任务有一对多的关联 任务与子任务有一对多关联。 阶段、任务和子任务表都有一个字段“计划结束日期”和“状态”

现在我想打印projectsindex操作中每个项目在计划结束日期之前未完成的总阶段、任务和子任务

在Rails模型中如何实现这一点

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
我试过的-

projectindex.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>
代码所做的是打印所有项目的挂起阶段、任务和子任务的总数,并打印每个项目的相同计数。我想为每个项目的总挂起阶段+任务+子任务打印挂起阶段+任务+子任务。
什么

是的,您计算的是所有项目,而不仅仅是当前项目

你想要的是

<% stages = Stage.where(project_id: project.id) %>
从性能的角度来看,更好的方法是让数据库为您提供计数

<% stage_counters = project.stages.where('planned_end_date < ?', Date.today).where(status: [0,2]).count %>
这将允许您执行project.tasks和project.sub_任务

请注意,您可能希望将这些计算移到模型中

has_many :stages, dependent: :destroy
has_many :tasks, through: :stages
has_many :sub_tasks, through: :tasks
class Project

  def incomplete_stages_count
    stages.where('planned_end_date < ?', Date.today).where(status: [0,2]).count
  end

是的,您计算的是所有项目,而不仅仅是当前项目

你想要的是

<% stages = Stage.where(project_id: project.id) %>
从性能的角度来看,更好的方法是让数据库为您提供计数

<% stage_counters = project.stages.where('planned_end_date < ?', Date.today).where(status: [0,2]).count %>
这将允许您执行project.tasks和project.sub_任务

请注意,您可能希望将这些计算移到模型中

has_many :stages, dependent: :destroy
has_many :tasks, through: :stages
has_many :sub_tasks, through: :tasks
class Project

  def incomplete_stages_count
    stages.where('planned_end_date < ?', Date.today).where(status: [0,2]).count
  end
class Project
  def incomplete_stages_count
    stages.incomplete.count
  end
end