use one query to get tag names for sites in browse

This commit is contained in:
Kyle Drake 2017-04-03 01:09:47 -07:00
parent 2b69e9cb8a
commit f9c885c23b
2 changed files with 11 additions and 3 deletions

View file

@ -15,6 +15,14 @@ get '/browse/?' do
@pagination_dataset = site_dataset @pagination_dataset = site_dataset
@sites = site_dataset.all @sites = site_dataset.all
site_ids = @sites.collect {|s| s[:id]}
tags = DB['select site_id,name from tags join sites_tags on tags.id=sites_tags.tag_id where site_id IN ?', site_ids].all
@site_tags = {}
site_ids.each do |site_id|
@site_tags[site_id] = tags.select {|t| t[:site_id] == site_id}.collect {|t| t[:name]}
end
if params[:tag] if params[:tag]
@title = "Sites tagged #{params[:tag]}" @title = "Sites tagged #{params[:tag]}"
end end

View file

@ -116,10 +116,10 @@
</a> </a>
</div> </div>
<div class="site-tags"> <div class="site-tags">
<% if site.tags.count > 0 %> <% if @site_tags[site.id].length > 0 %>
<i class="fa fa-tag"></i> <i class="fa fa-tag"></i>
<% site.tags.each_with_index do |tag, index| %> <% @site_tags[site.id].each_with_index do |tag, index| %>
<a class="" href="/browse?tag=<%= Rack::Utils.escape tag.name %>"><%= tag.name %></a><% if index != site.tags.length - 1 %>,<% end %> <a class="" href="/browse?tag=<%= Rack::Utils.escape tag %>"><%= tag %></a><% if index != @site_tags[site.id].length - 1 %>,<% end %>
<% end %> <% end %>
<% end %> <% end %>
</div> </div>