Sort on and index taggings, ftw

This commit is contained in:
Raphael Sofaer 2011-07-07 15:14:29 -07:00
parent d0ce257107
commit e98cc4b960
3 changed files with 12 additions and 3 deletions

View file

@ -59,12 +59,12 @@ class TagsController < ApplicationController
@tag = ActsAsTaggableOn::Tag.where(:name => params[:name]).first @tag = ActsAsTaggableOn::Tag.where(:name => params[:name]).first
if @tag if @tag
@posts = @posts.joins("LEFT OUTER JOIN comments ON comments.post_id = posts.id"). @posts = @posts.joins("LEFT OUTER JOIN comments ON comments.post_id = posts.id").
joins("INNER JOIN taggings ON (taggings.tag_id = #{@tag.id} AND joins("INNER JOIN taggings ON (taggings.tag_id = #{@tag.id} AND
((taggable_id = posts.id AND taggable_type = 'Post') OR (taggings.taggable_type = 'Comment' AND taggings.taggable_id = comments.id)))") ((taggable_id = posts.id AND taggable_type = 'Post') OR (taggings.taggable_type = 'Comment' AND taggings.taggable_id = comments.id)))")
max_time = params[:max_time] ? Time.at(params[:max_time].to_i) : Time.now max_time = params[:max_time] ? Time.at(params[:max_time].to_i) : Time.now
@posts = @posts.where(StatusMessage.arel_table[:created_at].lt(max_time)) @posts = @posts.where(StatusMessage.arel_table[:created_at].lt(max_time))
@posts = @posts.includes({:comments => {:author => :profile}}, :photos).order('posts.created_at DESC').limit(15) @posts = @posts.includes({:comments => {:author => :profile}}, :photos).order('taggings.created_at DESC').limit(15)
else else
@posts = [] @posts = []
end end

View file

@ -0,0 +1,8 @@
class IndexTaggingsCreatedAt < ActiveRecord::Migration
def self.up
add_index :taggings, :created_at
end
def self.down
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20110705003445) do ActiveRecord::Schema.define(:version => 20110707221112) do
create_table "aspect_memberships", :force => true do |t| create_table "aspect_memberships", :force => true do |t|
t.integer "aspect_id", :null => false t.integer "aspect_id", :null => false
@ -343,6 +343,7 @@ ActiveRecord::Schema.define(:version => 20110705003445) do
t.datetime "created_at" t.datetime "created_at"
end end
add_index "taggings", ["created_at"], :name => "index_taggings_on_created_at"
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context" add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
add_index "taggings", ["taggable_id", "taggable_type", "tag_id"], :name => "index_taggings_uniquely", :unique => true add_index "taggings", ["taggable_id", "taggable_type", "tag_id"], :name => "index_taggings_uniquely", :unique => true