cirandas.net

ref: master

db/migrate/026_adds_comment_count_to_article.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class AddsCommentCountToArticle < ActiveRecord::Migration
  def self.up
    add_column :articles, :comments_count, :integer, :default => 0
    add_column :article_versions, :comments_count, :integer

    execute "update articles set comments_count = (select count(*) from comments where comments.article_id = articles.id)"
  end

  def self.down
    remove_column :article_versions, :comments_count
    remove_column :articles, :comments_count
  end
end