From 26a2a67a71d407a3477ee00f366fa63fd81f2f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sun, 6 Sep 2015 22:51:34 +0200 Subject: [PATCH] refactors debate's tags related utility methods tags are already loaded, using them instead of extracting them again for each debate saves 50 query per index (2 query per debate in the page) --- app/models/debate.rb | 6 ++++-- app/views/shared/_tags.html.erb | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/debate.rb b/app/models/debate.rb index 5e2864767..6d6d49a2d 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -86,13 +86,15 @@ class Debate < ActiveRecord::Base end def tag_list_with_limit(limit = nil) - tags.most_used(limit).pluck :name + return tags if limit.blank? + + tags.sort{|a,b| b.taggings_count <=> a.taggings_count}[0, limit] end def tags_count_out_of_limit(limit = nil) return 0 unless limit - count = tags.count - limit + count = tags.size - limit count < 0 ? 0 : count end diff --git a/app/views/shared/_tags.html.erb b/app/views/shared/_tags.html.erb index 2b599ec77..ea3f65c6b 100644 --- a/app/views/shared/_tags.html.erb +++ b/app/views/shared/_tags.html.erb @@ -3,7 +3,7 @@ <% if debate.tags.any? %> <% debate.tag_list_with_limit(limit).each do |tag| %> - <%= link_to sanitize(tag), debates_path(tag: tag) %> + <%= link_to sanitize(tag.name), debates_path(tag: tag.name) %> <% end %> <% if debate.tags_count_out_of_limit(limit) > 0 %>