diff --git a/app/models/budget/stats.rb b/app/models/budget/stats.rb index 766e6ef77..cd9bff741 100644 --- a/app/models/budget/stats.rb +++ b/app/models/budget/stats.rb @@ -107,19 +107,19 @@ class Budget::Stats end def authors - budget.investments.pluck(:author_id) + @authors ||= budget.investments.pluck(:author_id) end def voters - supports(budget).distinct.pluck(:voter_id) + @voters ||= supports(budget).distinct.pluck(:voter_id) end def balloters - budget.ballots.where("ballot_lines_count > ?", 0).distinct.pluck(:user_id).compact + @balloters ||= budget.ballots.where("ballot_lines_count > ?", 0).distinct.pluck(:user_id).compact end def poll_ballot_voters - budget&.poll ? budget.poll.voters.pluck(:user_id) : [] + @poll_ballot_voters ||= budget.poll ? budget.poll.voters.pluck(:user_id) : [] end def balloters_by_heading(heading_id) @@ -174,8 +174,6 @@ class Budget::Stats end stats_cache(*stats_methods) - stats_cache :total_participants_with_gender - stats_cache :voters, :participants, :authors, :balloters, :poll_ballot_voters def stats_cache(key, &block) Rails.cache.fetch("budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}", &block) diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index 14cb1fa9d..0839f32fa 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -34,7 +34,7 @@ module Statisticable end def participants - User.unscoped.where(id: participant_ids) + @participants ||= User.unscoped.where(id: participant_ids) end def total_male_participants @@ -103,7 +103,7 @@ module Statisticable end def total_participants_with_gender - participants.where.not(gender: nil).distinct.count + @total_participants_with_gender ||= participants.where.not(gender: nil).distinct.count end def age_groups diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb index a6be63640..ea075e6c9 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -96,15 +96,14 @@ class Poll::Stats end def voters - poll.voters + @voters ||= poll.voters end def recounts - poll.recounts + @recounts ||= poll.recounts end stats_cache(*stats_methods) - stats_cache :participants, :voters, :recounts def stats_cache(key, &block) Rails.cache.fetch("polls_stats/#{poll.id}/#{key}/#{version}", &block)