rails 应用程序中的计数器未按预期方式工作
Counter does not working in expected way in rails application
我创建了一个 projet
脚手架,它与 user
有多对一的关联。项目脚手架与 stage
具有一对多关联,阶段与 sub_task
具有一对多关联。每个脚手架都有一个属性 planned_end_date
现在我想打印阶段数,阶段和 sub_task 未在选定日期完成。并将其打印到 project#index 操作的列中。此代码通过删除 @projects.each do |project|
在 project#show 操作中运行良好,但 project#index 操作中的相同代码在列的每一行中打印相同的数字。
projects_controller.rb(索引#动作)
@projects = current_user.projects.all.order("created_at DESC").paginate(page: params[:page], per_page: 15)
@projects.each do |project|
@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 = stage_counter + task_counter + sub_task_counter
end
项目#index
<table>
<thead>
<tr>
<th>Project Name</th>
<th>Activity Status</th>
</tr>
</thead>
<tbody>
<% @projects.each do |project| %>
<tr>
<td><%= project.project_name %></td>
<td><%= @count %></td>
</tr>
<% end %>
</tbody>
</table>
宝石文件
ruby '2.6.3'
gem 'rails', '~> 5.2.4'
我想打印未按时完成的每个项目的阶段、任务、sub_task 的总数。在 project#index 动作中。谁能知道我错过了什么???
@rock,首先,你没有正确使用ruby。
你不需要使用循环和计数器来代替,你可以这样做:
@projects = current_user.projects.all.order("created_at DESC").paginate(page: params[:page], per_page: 15)
@total_count = []
@projects.each do |project|
stages = project.stages.where('stages.planned_end_date < ?', Time.now).where('status IN (?)', [0,2])
tasks = project.tasks.where('tasks.planned_end_date < ?', Time.now).where('status IN (?)', [0,2])
sub_tasks = project.sub_tasks.where('sub_tasks.planned_end_date < ?', Time.now).where('status IN (?)', [0,2])
@total_count << { project_id: (stages.count + tasks.count + sub_tasks.count) }
end
输出将是:
[{1:20}, {2:25}, {4:30}, {7:10}]
这是一个散列数组,其中每个元素的键作为项目 ID,值作为该项目的阶段、任务和子任务的总和
我创建了一个 projet
脚手架,它与 user
有多对一的关联。项目脚手架与 stage
具有一对多关联,阶段与 sub_task
具有一对多关联。每个脚手架都有一个属性 planned_end_date
现在我想打印阶段数,阶段和 sub_task 未在选定日期完成。并将其打印到 project#index 操作的列中。此代码通过删除 @projects.each do |project|
在 project#show 操作中运行良好,但 project#index 操作中的相同代码在列的每一行中打印相同的数字。
projects_controller.rb(索引#动作)
@projects = current_user.projects.all.order("created_at DESC").paginate(page: params[:page], per_page: 15)
@projects.each do |project|
@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 = stage_counter + task_counter + sub_task_counter
end
项目#index
<table>
<thead>
<tr>
<th>Project Name</th>
<th>Activity Status</th>
</tr>
</thead>
<tbody>
<% @projects.each do |project| %>
<tr>
<td><%= project.project_name %></td>
<td><%= @count %></td>
</tr>
<% end %>
</tbody>
</table>
宝石文件
ruby '2.6.3'
gem 'rails', '~> 5.2.4'
我想打印未按时完成的每个项目的阶段、任务、sub_task 的总数。在 project#index 动作中。谁能知道我错过了什么???
@rock,首先,你没有正确使用ruby。
你不需要使用循环和计数器来代替,你可以这样做:
@projects = current_user.projects.all.order("created_at DESC").paginate(page: params[:page], per_page: 15)
@total_count = []
@projects.each do |project|
stages = project.stages.where('stages.planned_end_date < ?', Time.now).where('status IN (?)', [0,2])
tasks = project.tasks.where('tasks.planned_end_date < ?', Time.now).where('status IN (?)', [0,2])
sub_tasks = project.sub_tasks.where('sub_tasks.planned_end_date < ?', Time.now).where('status IN (?)', [0,2])
@total_count << { project_id: (stages.count + tasks.count + sub_tasks.count) }
end
输出将是:
[{1:20}, {2:25}, {4:30}, {7:10}] 这是一个散列数组,其中每个元素的键作为项目 ID,值作为该项目的阶段、任务和子任务的总和