Add excerpt column to topics table to remove N+1 query in ListableTopicSerializer

This commit is contained in:
Neil Lalonde
2014-03-18 13:40:40 -04:00
parent 3cf0adaed0
commit 0b1550f9d4
6 changed files with 50 additions and 5 deletions

View File

@@ -0,0 +1,18 @@
class AddExcerptToTopics < ActiveRecord::Migration
def up
add_column :topics, :excerpt, :string, limit: 1000
topic_ids = execute("SELECT id FROM topics WHERE pinned_at IS NOT NULL").map {|r| r['id'].to_i }
topic_ids.each do |topic_id|
cooked = execute("SELECT cooked FROM posts WHERE topic_id = #{topic_id} ORDER BY post_number ASC LIMIT 1")[0]['cooked']
if cooked
excerpt = ExcerptParser.get_excerpt(cooked, 220, strip_links: true)
execute "UPDATE topics SET excerpt = #{ActiveRecord::Base.sanitize(excerpt)} WHERE id = #{topic_id}"
end
end
end
def down
remove_column :topics, :excerpt
end
end