rename_column :table, :old_column, :new_column
You'll probably want to create a separate migration to do this. (Rename FixColumnName
as you will.):
script/generate migration FixColumnName
# creates db/migrate/xxxxxxxxxx_fix_column_name.rb
Then edit the migration to do your will:
# db/migrate/xxxxxxxxxx_fix_column_name.rb
class FixColumnName < ActiveRecord::Migration
def self.up
rename_column :table_name, :old_column, :new_column
end
def self.down
# rename back if you need or do something else or do nothing
end
end
For Rails 3.1 use:
While, the up
and down
methods still apply, Rails 3.1 receives a change
method that "knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate down method".
See "Active Record Migrations" for more information.
rails g migration FixColumnName
class FixColumnName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
If you happen to have a whole bunch of columns to rename, or something that would have required repeating the table name over and over again:
rename_column :table_name, :old_column1, :new_column1
rename_column :table_name, :old_column2, :new_column2
...
You could use change_table
to keep things a little neater:
class FixColumnNames < ActiveRecord::Migration
def change
change_table :table_name do |t|
t.rename :old_column1, :new_column1
t.rename :old_column2, :new_column2
...
end
end
end
Then just db:migrate
as usual or however you go about your business.
For Rails 4:
While creating a Migration
for renaming a column, Rails 4 generates a change
method instead of up
and down
as mentioned in the above section. The generated change
method is:
$ > rails g migration ChangeColumnName
which will create a migration file similar to:
class ChangeColumnName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
Heroku links your projects based on the heroku
git remote (and a few other options, see the update below). To add your Heroku remote as a remote in your current repository, use the following command:
git remote add heroku git@heroku.com:project.git
where project
is the name of your Heroku project (the same as the project.heroku.com
subdomain). Once you've done so, you can use the heroku xxxx
commands (assuming you have the Heroku Toolbelt installed), and can push to Heroku as usual via git push heroku master
. As a shortcut, if you're using the command line tool, you can type:
heroku git:remote -a project
where, again, project
is the name of your Heroku project (thanks, Colonel Panic). You can name the Git remote anything you want by passing -r remote_name
.
[Update]
As mentioned by Ben in the comments, the remote doesn't need to be named heroku
for the gem commands to work. I checked the source, and it appears it works like this:
- If you specify an app name via the
--app
option (e.g. heroku info --app myapp
), it will use that app.
- If you specify a Git remote name via the
--remote
option (e.g. heroku info --remote production
), it will use the app associated with that Git remote.
- If you specify no option and you have
heroku.remote
set in your Git config file, it will use the app associated with that remote (for example, to set the default remote to "production" use git config heroku.remote production
in your repository, and Heroku will run git config heroku.remote
to read the value of this setting)
- If you specify no option, the gem finds no configuration in your
.git/config
file, and the gem only finds one remote in your Git remotes that has "heroku.com" in the URL, it will use that remote.
- If none of these work, it raises an error instructing you to pass
--app
to your command.
Best Answer
I needed to combine several solutions to make this work, here is what I did:
Gemfile
in my Heroku console
production.rb
I didn't need to precompile the assets locally.