cirandas.net

ref: master

db/migrate/20151210230319_add_followers_count_to_article.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class AddFollowersCountToArticle < ActiveRecord::Migration
  def self.up
    add_column :articles, :followers_count, :integer, :default => 0
    execute "update articles set followers_count = (select count(*) from article_followers where article_followers.article_id = articles.id)"
  end

  def self.down
    remove_column :articles, :followers_count
  end
end