Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
Refactor queries for related items to do it all in one join rather th…
Browse files Browse the repository at this point in the history
…an many selects to retrieve the individual items
  • Loading branch information
aaronpk committed Jan 5, 2012
1 parent cb02807 commit 30ee74e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
12 changes: 8 additions & 4 deletions controllers/photos.rb
Expand Up @@ -283,10 +283,14 @@

def load_related_photos
@photo_ids = @photos.collect {|p| p.id}
@related_sets = PhotoPhotoset.all(:photo_id => @photo_ids, :fields => [:photoset_id], :unique => true).to_a.reject{|a| a.photoset.nil?}.sort_by!{|a| -a.photoset.num}
@related_tags = PhotoTag.all(:photo_id => @photo_ids, :fields => [:tag_id], :unique => true).to_a.reject{|a| a.tag.nil?}.sort_by!{|a| -a.tag.num}
@related_people = PersonPhoto.all(:photo_id => @photo_ids, :fields => [:person_id], :unique => true).to_a.reject{|a| a.person.nil?}.sort_by! {|a| -a.person.num}
@related_places = PhotoPlace.all(:photo_id => @photo_ids, :fields => [:place_id], :unique => true).to_a.reject{|a| a.place.nil?}.sort_by! {|a| -a.place.num}
@related_sets = Photoset.all(Photoset.photos.id => @photo_ids, :order => :num.desc)
@related_tags = Tag.all(Tag.photos.id => @photo_ids, :order => :num.desc)
@related_people = Person.all(Person.photos.id => @photo_ids, :order => :num.desc)
@related_places = Place.all(Place.photos.id => @photo_ids, :order => :num.desc)
# @related_sets = PhotoPhotoset.all(:photo_id => @photo_ids, :fields => [:photoset_id], :unique => true).to_a.reject{|a| a.photoset.nil?}.sort_by!{|a| -a.photoset.num}
# @related_tags = PhotoTag.all(:photo_id => @photo_ids, :fields => [:tag_id], :unique => true).to_a.reject{|a| a.tag.nil?}.sort_by!{|a| -a.tag.num}
# @related_people = PersonPhoto.all(:photo_id => @photo_ids, :fields => [:person_id], :unique => true).to_a.reject{|a| a.person.nil?}.sort_by! {|a| -a.person.num}
# @related_places = PhotoPlace.all(:photo_id => @photo_ids, :fields => [:place_id], :unique => true).to_a.reject{|a| a.place.nil?}.sort_by! {|a| -a.place.num}
true
end

Expand Down
16 changes: 8 additions & 8 deletions views/photos/list.erb
Expand Up @@ -39,8 +39,8 @@
<div class="sidebar-widget related">
<h3><%= @related_titles[:sets] %></h3>
<ul>
<% @related_sets.each do |link| next if !link.photoset %>
<li><a href="<%= link.photoset.page %>"><%= link.photoset.display_name %> <span class="num">(<%= link.photoset.num %>)</span></a></li>
<% @related_sets.each do |photoset| %>
<li><a href="<%= photoset.page %>"><%= photoset.display_name %> <span class="num">(<%= photoset.num %>)</span></a></li>
<% end %>
</ul>
</div>
Expand All @@ -50,8 +50,8 @@
<div class="sidebar-widget related">
<h3><%= @related_titles[:people] %></h3>
<ul>
<% @related_people.each do |link| next if !link.person %>
<li><a href="<%= link.person.page %>"><%= link.person.display_name %></a> <span class="num">(<%= link.person.num %>)</span></li>
<% @related_people.each do |person| %>
<li><a href="<%= person.page %>"><%= person.display_name %></a> <span class="num">(<%= person.num %>)</span></li>
<% end %>
</ul>
</div>
Expand All @@ -61,8 +61,8 @@
<div class="sidebar-widget related">
<h3><%= @related_titles[:places] %></h3>
<ul>
<% @related_places.each do |link| next if !link.place %>
<li><a href="<%= link.place.page %>"><%= link.place.display_name %></a> <span class="num">(<%= link.place.num %>)</span></li>
<% @related_places.each do |place| %>
<li><a href="<%= place.page %>"><%= place.display_name %></a> <span class="num">(<%= place.num %>)</span></li>
<% end %>
</ul>
</div>
Expand All @@ -89,8 +89,8 @@
<div class="sidebar-widget related">
<h3><%= @related_titles[:tags] %></h3>
<ul>
<% @related_tags.each do |link| next if !link.tag %>
<li><a href="<%= link.tag.page %>"><%= link.tag.display_name %></a> <span class="num">(<%= link.tag.num %>)</span></li>
<% @related_tags.each do |tag| %>
<li><a href="<%= tag.page %>"><%= tag.display_name %></a> <span class="num">(<%= tag.num %>)</span></li>
<% end %>
</ul>
</div>
Expand Down

0 comments on commit 30ee74e

Please sign in to comment.