【问题标题】:Adding extra SQL queries in a Redmine core view (overriding view in plugin)在 Redmine 核心视图中添加额外的 SQL 查询(插件中的覆盖视图)
【发布时间】:2009-11-26 14:52:55
【问题描述】:

我正在我的插件 (/views/reports/_details.rhtml) 中覆盖 Redmine 核心视图。只是为了打印一些额外的数据,例如分配了多少未分配的问题。

我尝试重写控制器并添加一个方法来执行此操作,但我从未让它工作,所以我将下面的代码添加到视图中(是的,它很丑,但页面很少被使用)。

现在我的问题是,视图在所有跟踪器 (row.id) 中循环并显示信息,例如打开和关闭的问题数量。所以我添加了一个额外的 SQL 查询,但它只适用于第一次跟踪器迭代,其余的它会一遍又一遍地显示相同的数据。

当我查看 development.log 时,其中只有一个 SQL 查询。但是当我输出 row.id () 时,它会显示每个跟踪器的正确值。

我应该如何解决这个问题?

_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...)

【问题讨论】:

    标签: ruby-on-rails redmine


    【解决方案1】:

    我通过添加两个 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)) %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多