代码之家  ›  专栏  ›  技术社区  ›  Johan Olsson

在Redmine核心视图中添加额外的SQL查询(在插件中覆盖视图)

  •  2
  • Johan Olsson  · 技术社区  · 16 年前

    我正在重写插件中的Redmine核心视图(/views/reports/\u details.rhtml)。只是打印一些额外的数据,比如分配了多少未解决的问题而没有分配。

    我尝试重写控制器并添加一个方法来完成这项工作,但是我一直没有使它工作,所以我将下面的代码添加到视图中(是的,它很难看,但很少使用页面)。

    现在,对于我的问题,视图通过所有跟踪程序(row.id)循环显示信息,比如打开和关闭了多少个问题。所以我添加了一个额外的SQL查询,但它只适用于第一个跟踪器迭代,其余的则一遍又一遍地显示相同的数据。

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

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

    代码输入详细信息.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
    

    (以前从来没有在铁轨上使用过红宝石或红矿…)

    1 回复  |  直到 16 年前
        1
  •  0
  •   Johan Olsson    16 年前

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