How to rename a database column in Ruby on Rails?

Small tutorial on how to rename a database column in Ruby on Rails with code examples
26 September 2017   3561

Let's imagine an issue. For example, you needed to create a database column in Rails, called hype.codes. And you mistyped it as hyde.codes. How to solve this issue? Let's figure it out!

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
# it 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
  end
end

Small update for Rails 3.1

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"

rails g migration FixColumnName

class FixColumnName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column, :new_column
  end
end

If you need 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 

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

Small update for Rails 4

While creating a Migration as for renaming a column, Rails 4 generates a change method instead of up and down as mentioned in the above answer. The generated change method is as below :

$ > rails g migration ChangeColumnName

which will create a migration file similar to this :

class ChangeColumnName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column, :new_column
  end
end

Ruby\RoR News Digest 3 - 9.08

This week's freshest news, updates and guides from the Ruby and Rails programming
09 August 2019   417

Greetings! I hope your week went great! Here's new Ruby and Ruby on Rails news digest.

As always, we have prepared some great material for you. In this issue, you will learn, for example, how to migrate your Docker\Kubernetes Ruby app today, how to use Ruby unique method to remove duplicates, watch video on how to remove conditionals with Ruby's Array wrap method and many other things.

Guides

  • Don’t change the signature of Sidekiq jobs running in production

Author brings a lot of arguments to the statement, given in the header.

  • Fullstaq Ruby: First impressions, and how to migrate your Docker/Kubernetes Ruby apps today

Short, but interesting tutorial, which covers one of the aspects of Fullstaq Ruby.

  • How to Use The Ruby Uniq Method To Remove Duplicates

 Using uniq with a block provides even more power; great guide for the beginners.

Videos

  • Removing conditionals with Ruby's Array wrap method

  • Dev Quick Tips - Using Ruby's Reduce method to create a Hash of "Banana Bunch" Arrays

Article

  • Tales of the Ruby Grimoire - Part One - The Grimoire

As author of this programming fairytale explains, this is the first of many tales of the legendary Ruby Grimoire, a great and terrible book of Ruby dark magics

  • Introducing the ‘aws-rails-provisioner’ gem developer preview

This gem is designed to help you define and deploy containerized Rails apps on AWS Fargate

Updates

  • Jekyll 4.0.0.pre.beta1 Released 

Pre beta1 of new major version of the solution, that allows to transform text in static websites and blogs

  • Ciao: An HTTP Monitoring Tool, Built on Rails 

Webapp that checks HTTP endpoints and can send notifications when something bad happens

  • Hightop: A Shortcut for Group Count Queries

Adds a top method to Enumberable so works with both plain old arrays or ActiveRecord.

Podcast

  • RR 424: Documenting Your Code

David Kumira (Screencaster of Drifting Ruby, Panelist on Ruby Rogues), Nate Hopkins (CodeFund) and Andrew Mason (Full stack Ruby on Rails developer) talks why documenting your Ruby (and not only Ruby) code is valuable.