From c8b9d96f66c8ab96fe4b7394867be2d37a5d30fa Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Wed, 5 Oct 2016 15:05:25 +0300 Subject: [PATCH] Optimize number of conflicts calculation --- app/models/conflict_coefficient.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/conflict_coefficient.rb b/app/models/conflict_coefficient.rb index 9ad0f66..0245e2a 100644 --- a/app/models/conflict_coefficient.rb +++ b/app/models/conflict_coefficient.rb @@ -19,9 +19,11 @@ class ConflictCoefficient end def conflicts - @conflicts ||= talk_preferences.select do |talk_preference| - talk_preference.include_all? [@left, @right] - end.count + talk_preferences.joins('INNER JOIN "selected_talks" AS left + ON "left"."talk_preference_id" = "talk_preferences"."unique_id" + INNER JOIN "selected_talks" AS right + ON "right"."talk_preference_id" = "talk_preferences"."unique_id"') + .where(left: {talk_id: @left}, right: {talk_id: @right}).count end def total_votes