Improve User#headings_supported_within_group performance

Performs a single DB call instead of 3
This commit is contained in:
Marko Lovic
2018-07-10 09:36:27 +02:00
committed by decabeza
parent 3c9953e9e0
commit 264c4e747b

View File

@@ -128,8 +128,13 @@ class User < ActiveRecord::Base
end
def headings_voted_within_group(group)
voted_investments = votes.for_budget_investments(Budget::Investment.by_group(group.id)).votables
Budget::Heading.where(id: voted_investments.map(&:heading_id).uniq)
Budget::Heading.where(id:
votes.where(votable_type: Budget::Investment)
.joins(:budget_investment)
.where(budget_investments: {group_id: group.id})
.distinct
.select('budget_investments.heading_id')
)
end
def administrator?