# Preloads visible total spent time for a collection of issues
def self.load_visible_spent_hours(issues, user=User.current)
if issues.any?
- hours_by_issue_id = TimeEntry.visible(user).group(:issue_id).sum(:hours)
+ hours_by_issue_id = TimeEntry.visible(user).where(:issue_id => issues.map(&:id)).group(:issue_id).sum(:hours)
issues.each do |issue|
issue.instance_variable_set "@spent_hours", (hours_by_issue_id[issue.id] || 0)
end
def self.load_visible_total_spent_hours(issues, user=User.current)
if issues.any?
- hours_by_issue_id = TimeEntry.visible(user).joins(:issue).joins("JOIN #{Issue.table_name} parent ON parent.root_id = #{Issue.table_name}.root_id" +
- " AND parent.lft <= #{Issue.table_name}.lft AND parent.rgt >= #{Issue.table_name}.rgt").group("parent.id").sum(:hours)
+ hours_by_issue_id = TimeEntry.visible(user).joins(:issue).
+ joins("JOIN #{Issue.table_name} parent ON parent.root_id = #{Issue.table_name}.root_id" +
+ " AND parent.lft <= #{Issue.table_name}.lft AND parent.rgt >= #{Issue.table_name}.rgt").
+ where("parent.id IN (?)", issues.map(&:id)).group("parent.id").sum(:hours)
issues.each do |issue|
issue.instance_variable_set "@total_spent_hours", (hours_by_issue_id[issue.id] || 0)
end