reduce db quries on aspects#index

This commit is contained in:
MrZYX 2011-03-27 17:22:35 +02:00
parent 52fee96bfc
commit 3af5e24ac6
3 changed files with 11 additions and 3 deletions

View file

@ -31,7 +31,7 @@ class AspectsController < ApplicationController
@aspect_ids = @aspects.map{|a| a.id}
@posts = StatusMessage.joins(:aspects).where(:pending => false,
:aspects => {:id => @aspect_ids}).includes(:comments, :photos, :likes, :dislikes).select('DISTINCT `posts`.*').paginate(
:aspects => {:id => @aspect_ids}).includes(:aspects, :post_visibilities, :comments, :photos, :likes, :dislikes).select('DISTINCT `posts`.*').paginate(
:page => params[:page], :per_page => 15, :order => sort_order + ' DESC')
@fakes = PostsFake.new(@posts)

View file

@ -21,13 +21,13 @@ module ApplicationHelper
def aspects_with_post aspects, post
aspects.select do |aspect|
PostVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id)
aspect.has_post?(post)
end
end
def aspects_without_post aspects, post
aspects.reject do |aspect|
PostVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id)
aspect.has_post?(post)
end
end

View file

@ -21,6 +21,14 @@ class Aspect < ActiveRecord::Base
name.strip!
end
def has_post? post
post_ids = post_visibilities.each { |pv| pv.post_id }
post_ids.each { |id|
return true if id == post.id
}
return false
end
def to_s
name
end