25

I have this table:

class CreateShoes < ActiveRecord::Migration
  def change
    create_table :shoes do |t|
      t.string :name
      t.boolean :leather
      t.integer :season

      t.timestamps null: false
    end
  end
end

the 'season' column should be called 'season_id'. I know that I have to write 't.rename :season, :season_id' as explained in http://edgeguides.rubyonrails.org/active_record_migrations.html#column-modifiers but I don't manage to find the right syntax. Should it be?

class CreateShoes < ActiveRecord::Migration
  def change
    create_table :shoes do |t|
      t.string :name
      t.boolean :leather
      t.integer :season

      t.timestamps null: false
    end

    change_table :products do |t|
      t.rename :season, :season_id
    end

  end
end

Doesn't work. Anything I have to do in the Mac console? Thanks!

3
  • 4
    Why are you renaming it in the same migration? Either rollback and fix it in the create_table, or create a new migration. You can also just write rename_column :shoes, :season, :season_id instead of putting it in a block. Commented Jan 14, 2016 at 18:00
  • I don't quite know what I doing. learning :) forgot to add that the table has data in it. create new migration means a new 'def change'? thx Commented Jan 14, 2016 at 18:04
  • 1
    It means a new migration that has its own change method. Commented Jan 14, 2016 at 19:00

4 Answers 4

69

Run in your console:

$ rails g migration rename_season_to_season_id

Now file db/migrate/TIMESTAMP_rename_season_to_season_id.rb contains following:

class RenameSeasonToSeasonId < ActiveRecord::Migration
  def change
  end
end

Modify it as follows:

class RenameSeasonToSeasonId < ActiveRecord::Migration
  def change
    rename_column :shoes, :season, :season_id
  end
end

Then run $ rake db:migrate in console.

Sign up to request clarification or add additional context in comments.

Comments

5

Either fix your migration and do

rake db:rollback db:migrate

or make another migration like so:

rename_column :shoes, :season, :season_id if column_exists?(:shoes, :season) && !column_exists?(:shoes, :season_id)

and then do

rake db:migrate

Comments

1

If your intention is to rename column in table than you example migration is not making sense :)... Instead of this

class CreateShoes < ActiveRecord::Migration
  def change
    create_table :shoes do |t|
      t.string :name
      t.boolean :leather
      t.integer :season

      t.timestamps null: false
    end

    change_table :products do |t|
      t.rename :season, :season_id
    end

  end
end

You just need table change migration, like this(Rails will take care rollback for your):

class RenameSessionColumnInsideShoes < ActiveRecord::Migration
  def change
    change_table :products do |t|
      t.rename :season, :season_id
    end
  end
end

rename method on table object in rails is valid method, as you can see in Rails source code

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L582

Comments

1

If your branch has been pushed to production then one probably needs to add the new migration by running the following command:

rails g migration RenameSeasonColumnNameToShoes

and if it hasn't been pushed to production or you have to currently make changes to your branch only, then do:

bundle exec rake db:rollback

Then make changes to your migration file inside /db/migrate/<your_migration_file_name>

Then in your migration file, using rename_column do as follows:

class RenameSeasonColumnNameToShoes < ActiveRecord::Migration
  def change
      rename_column :shoes, :season, :season_id
  end
end

and then do

bundle exec rake db:migrate db:test:prepare

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.