cirandas.net

ref: master

db/migrate/20140709224246_create_real_relation_between_article_and_author.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class CreateRealRelationBetweenArticleAndAuthor < ActiveRecord::Migration
  def self.up
    add_column :articles, :author_id, :integer
    add_column :article_versions, :author_id, :integer

    # Set article's author as the first version's last_changed_by_id.
    execute("UPDATE article_versions SET author_id = last_changed_by_id")

    execute("UPDATE articles SET author_id = article_versions.author_id FROM article_versions WHERE article_versions.article_id = articles.id AND article_versions.version = 1")
 end

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