16

我想将新列添加sum(time_entries.hours)到 sql select 我正在查询这样的条目:

issues = Issue.visible.where(options[:conditions]).all(
    :include => ([:status, :project, :time_entries] + (options[:include] || [])).uniq,
    :conditions => statement,
    :order => order_option,
    :joins => query_joins(order_option.join(',')),
    :limit => options[:limit],
    :offset => options[:offset],
    :group => "#{Issue.table_name}.id"
)

它生成这个选择:

SELECT
 "issues"."id"                         AS t0_r0,
 ...
 "time_entries"."hours"                AS t3_r4,
 ...
 "versions"."sharing"                  AS t8_r9
FROM "issues"
 LEFT OUTER JOIN "projects" ON "projects"."id" = "issues"."project_id"
 LEFT OUTER JOIN "issue_statuses" ON "issue_statuses"."id" = "issues"."status_id"
 LEFT OUTER JOIN "time_entries" ON "time_entries"."issue_id" = "issues"."id"
 LEFT OUTER JOIN "users" ON "users"."id" = "issues"."assigned_to_id"
 LEFT OUTER JOIN "trackers" ON "trackers"."id" = "issues"."tracker_id"
 LEFT OUTER JOIN "enumerations"
   ON "enumerations"."id" = "issues"."priority_id" AND "enumerations"."type" IN ('IssuePriority')
 LEFT OUTER JOIN "issue_categories" ON "issue_categories"."id" = "issues"."category_id"
 LEFT OUTER JOIN "versions" ON "versions"."id" = "issues"."fixed_version_id"
WHERE "issues"."id" IN (1) AND (projects.status <> 9 AND projects.id IN (SELECT
                                                                       em.project_id
                                                                     FROM enabled_modules em
                                                                     WHERE em.name =     'issue_tracking')) AND
  ((issues.status_id IN (SELECT
                           id
                         FROM issue_statuses
                         WHERE is_closed = 'f')))
GROUP BY issues.id
ORDER BY issues.id
 DESC

我尝试将:select=>"*, sum(time_entries.hours)"哈希参数插入到all方法中,但没有效果。

如何向此选择添加新列?我还需要保留所有当前列,因为它们用于过滤器。有没有办法不手动指定所有列?

更新: Ruby 1.9.3 Rails 3.2.13

4

2 回答 2

14

表名看起来像 Redmine 中的。我最近实际上遇到了类似的问题。我发现选定的列来自可见范围。我无法确定确切的原因,但如果没有它,“选择”选项会按预期工作。

使用

Issue.select("#{Issue.table_name}.*, sum(#{TimeEntries.table_name}.hours AS total_hours").where("#{your_conditions}").group(:id).all

应该给你你想要的数据。附加列可以作为问题属性直接访问:

Issue.total_hours

编辑:

如果无法同时选择所有问题字段(用于创建完整的问题对象)和总和,则需要在获得所需问题后使用第二条语句。

issues = Issue.visible.where("#{issue_conditions}").all
issue_ids = issues.map(&:id)
# if you do not need the issue objects, use .pluck(:id) instead of .all

spent_hours = TimeEntries.select('issue_id, sum(hours)')
                         .where(:issue_id => issue_ids)
                         .where("#{other_time_conditions}")
                         .group(:issue_id).all

或者,您可以使用 Redmines Issue 类中包含的便利功能。使用它使软件为您完成工作:

i = Issue.includes(:time_entries).find(1)
i.total_spent_hours

包含确保计算不会再次命中数据库,因为所有相关关联都已加载。

于 2013-09-11T21:06:41.070 回答
4

有点旧,但这样的东西应该可以工作。(在 Rails 5 中)

issues = Issue. ...

issues.select(
  issues.arel.projections,
  TimeEntry.arel_table[:hours].sum
)
于 2018-12-08T00:03:09.130 回答