Update 20131017014509_add_post_count_to_categories.rb

In case the category does not have any posts yet. You get a null value error running the migration if this is the case.
This commit is contained in:
Erik Ordway 2013-10-21 09:28:35 -07:00
parent d10f9f756f
commit fe0dc8161a
1 changed files with 1 additions and 1 deletions

View File

@ -3,7 +3,7 @@ class AddPostCountToCategories < ActiveRecord::Migration
add_column :categories, :post_count, :integer, null: false, default: 0 add_column :categories, :post_count, :integer, null: false, default: 0
execute <<SQL execute <<SQL
UPDATE categories UPDATE categories
SET post_count = (SELECT SUM(posts_count) FROM topics SET post_count = (SELECT COALESCE(SUM(posts_count),0) FROM topics
WHERE category_id = categories.id AND deleted_at IS NULL) WHERE category_id = categories.id AND deleted_at IS NULL)
SQL SQL
end end