Sha256: 8df64fd0e620b9a367dcc36dcb8a8a0a22d56fe4add57503324fdf5dfcb6791d

Contents?: true

Size: 905 Bytes

Versions: 17

Compression:

Stored size: 905 Bytes

Contents

class MakeCommentsPolymorphic < ActiveRecord::Migration

  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

  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
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
tkh_content-0.10.10 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10.9 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10.8 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10.7 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10.6 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10.5.1 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10.5 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10.4 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10.3 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10.2 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10.1 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.10 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.9.20 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.9.19 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.9.18 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.9.17 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb
tkh_content-0.9.16 lib/generators/tkh_content/create_or_update_migrations/templates/make_comments_polymorphic.rb