Question

So I generated a migration to change the :comments of my Reviews table from string to text.

class ChangeDataTypeForReviews < ActiveRecord::Migration
  def self.up
    change_table :reviews do |t|
      t.change :comments, :text
    end
  end
  def self.down
    change_table :reviews do |t|
      t.change :comments, :string
    end
  end
end

After running rake db:migrate, it worked and did change it to text, but I was wondering why in my actual createReviews migration, comments is still a string, and does this matter, or should I change it to text?

   class CreateReviews < ActiveRecord::Migration
      def change
        create_table :reviews do |t|
          t.string :artist
          t.string :comments

          t.timestamps
        end
      end
    end
Was it helpful?

Solution

Hope you have run this,

class ChangeDataTypeForReviews < ActiveRecord::Migration
  def self.up
    change_table :reviews do |t|
      t.change :comments, :text
    end
  end
  def self.down
    change_table :reviews do |t|
      t.change :comments, :string
    end
  end
end

after executing,

  class CreateReviews < ActiveRecord::Migration
    def change
      create_table :reviews do |t|
        t.string :artist
        t.string :comments

        t.timestamps
      end
    end
  end

So, the latest change will be reflected in the corresponding table. It will not change anything in the previously created migration file. I.e., why we call it as Migration, right?

So, no need to change t.string :comments, because with the latest migration, we did change the datatype to text. So, it will always be interpreted as a text type field only.

Go through the docs properly.

Hope it helps :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top