Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>rename_column :table, :old_column, :new_column </code></pre> <p><strong>Update:</strong></p> <p>You'll probably want to create a separate migration to do this. (Rename FixColumnName as you will)</p> <pre><code>script/generate migration FixColumnName # creates db/migrate/xxxxxxxxxx_fix_column_name.rb </code></pre> <p>Then edit the migration to do your will.</p> <pre><code># db/migrate/xxxxxxxxxx_fix_column_name.rb class FixColumnName &lt; 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 </code></pre> <hr> <p><strong>An update for Rails 3.1</strong></p> <p>While, the <code>up</code> and <code>down</code> methods still apply. Rails 3.1 receives a <code>change</code> method that <a href="http://guides.rubyonrails.org/migrations.html" rel="noreferrer">"knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate down method"</a></p> <pre><code>rails g migration FixColumnName class FixColumnName &lt; ActiveRecord::Migration def change rename_column :table_name, :old_column, :new_column end end </code></pre> <hr> <p>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.</p> <pre><code>rename_column :table_name, :old_column1, :new_column1 rename_column :table_name, :old_column2, :new_column2 ... </code></pre> <p>You could use <code>change_table</code> to keep things a little neater.</p> <pre><code>class FixColumnNames &lt; 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 </code></pre> <p><em>Thank you, <code>Luke</code> &amp;&amp; <code>Turadg</code>, for bringing up the topic.</em></p> <p>Then just <code>db:migrate</code> as usual or however you go about your business.</p> <hr> <p><strong>An update for Rails 4</strong></p> <p>While creating a <code>Migration</code> as for renaming a column, Rails 4 generates a <code>change</code> method instead of <code>up</code> and <code>down</code> as mentioned in the above answer. The generated <code>change</code> method is as below : </p> <pre><code>$ &gt; rails g migration ChangeColumnName </code></pre> <p>which will create a migration file similar to this :</p> <pre><code>class ChangeColumnName &lt; ActiveRecord::Migration def change rename_column :table_name, :old_column, :new_column end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload