ralphxiaoz
ralphxiaoz

Reputation: 101

Rename columns after migration in rails

I wonder if there's a fast way that I can update the column names after I did the migration in rails?

For example, in my schema, entity user has a column "user_name", and I changed that to "name" through a migration below:

class FixColumnName < ActiveRecord::Migration
  def change
    rename_column :users, :user_name, :name
  end
end

Then I ran

rake db:migrate

However, in other files, say test/users_controller_test.rb, the column name is still "user_name", and I have to modify that manually. I wonder if there's a way to change the name for good?

Upvotes: 0

Views: 445

Answers (2)

neo
neo

Reputation: 4116

Run vim some path/test/users_controller_test.rb on your terminal Press escape Type: :%s/user_name/name/g hit enter Escape again, type :x and hit enter

If you don't want to use vim, I'm sure there's a way to do in whatever text editor you're using. Search find and replace with the name of your editor on Google.

Upvotes: 0

patrick
patrick

Reputation: 10273

I don't believe there's any way, other than destroying the entire generation, to do this using CLI.

You should use your editor to either find/replace or do more advanced refactoring. For example, in RubyMine, which I use, there is a quite a comprehensive refactoring capability. You can read about it here.

Goodluck!

Upvotes: 1

Related Questions