在Redmine核心视图中添加额外的SQL查询(在插件中覆盖视图)
-
05-07-2019 - |
题
我在我的插件中覆盖了Redmine核心视图(/views/reports/_details.rhtml)。只是为了打印一些额外的数据,比如分配了多少未公开的问题而没有分配。
我尝试重写控制器并添加一个方法来执行此操作,但我从来没有让它工作,所以我将下面的代码添加到视图中(是的,它很难看,但很少使用页面)。
现在我的问题是,视图循环遍历所有跟踪器(row.id)并显示信息,例如打开和关闭的问题数量。所以我添加了一个额外的SQL查询,但它只适用于第一个跟踪器迭代,其余的则一遍又一遍地显示相同的数据。
当我查看development.log时,其中只有一个SQL查询。但是当我输出row.id(<!> lt;%= row.id%<!> gt;)时,它会显示每个跟踪器的正确值。
我该如何解决这个问题?
_details.rhtml中的代码
<% @total_assigned_and_open ||=
ActiveRecord::Base.connection.select_all("select count(i.id) as total
from
#{Issue.table_name} i, #{IssueStatus.table_name} s, #{Tracker.table_name} t
where
i.status_id=s.id
and i.tracker_id=#{row.id}
and i.project_id=#{@project.id}
and i.assigned_to_id is null
and s.is_closed = 0
group by s.id, s.is_closed, t.id limit 1") %>
development.log中的SQL查询
select count(i.id) as total
from
issues i, issue_statuses s, trackers t
where
i.status_id=s.id
and i.tracker_id=1
and i.project_id=1
and i.assigned_to_id is null
and s.is_closed = 0
group by s.id, s.is_closed, t.id limit 1
(从未在之前使用Ruby on Rails或Redmine)。
解决方案
我通过添加两个SQL查询(在视图中)来解决它,该查询选择了所选项目的所有问题。
<% @all_unsigned_issues ||=
ActiveRecord::Base.connection.select_all("select i.status_id as status, s.is_closed as
closed, t.id as tracker_id, count(i.id) as total
from
#{Issue.table_name} i, #{IssueStatus.table_name} s, #{Tracker.table_name} t
where
i.assigned_to_id is null
and i.project_id=#{@project.id}
and i.status_id=s.id
and i.tracker_id=t.id
group by i.id") %>
然后我用。
<%= aggregate_link @all_unsigned_issues, { "tracker_id" => row.id, "status" => 5 },
:controller => 'issues', :action => 'index', :project_id => ((row.is_a?(Project) ? row : @project)) %>
不隶属于 StackOverflow