Rails - 活动记录:添加额外的选择列以查找(:全部)

时间:2013-09-11 20:02:17

标签: ruby-on-rails activerecord rails-activerecord

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

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

2 个答案:

答案 0 :(得分:9)

表格名称与Redmine相似。我最近遇到了类似的问题。我发现所选择的柱子来自可见范围。我无法确定原因,但没有它,“选择”选项按预期工作。

使用

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

应该为您提供所需的数据。附加列可以直接作为Issue属性访问:

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

include确保计算不会再次访问数据库,因为已加载所有相关关联。

答案 1 :(得分:2)

有点旧,但类似的东西应该起作用。 (在Rails 5中)

issues = Issue. ...

issues.select(
  issues.arel.projections,
  TimeEntry.arel_table[:hours].sum
)