Class: MakeCommentsPolymorphic

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb

Class Method Summary collapse

Class Method Details

.downObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb', line 21

def self.down
  add_column :comments, :page_id
  add_index :comments, :page_id

  Comment.all.each do |comment|
    if comment.commentable_type == 'Page'
      comment.page_id = comment.commentable_id
      comment.save
    end
  end

  remove_column :comments, :commentable_type, :string
  remove_column :comments, :commentable_id, :integer

end

.upObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb', line 3

def self.up
  add_column :comments, :commentable_type, :string
  add_column :comments, :commentable_id, :integer
  add_index :comments, :commentable_id

  Comment.all.each do |comment|
    unless comment.page_id.blank?
      comment.commentable_type = 'Page'
      comment.commentable_id = comment.page_id
      comment.save
    end
  end

  # Clean up comment table. The index is removed automatically
  remove_column :comments, :page_id

end