我想将新列添加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