0

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?

ralphxiaoz
  • 101
  • 1
  • 6
  • If this table was created using rails generate and you are just getting started with the app you can [use rails destroy](http://stackoverflow.com/questions/4161357/how-do-i-reverse-a-rails-generate) to undo the entire generation. – Antarr Byrd Mar 03 '15 at 02:24
  • Thanks, but I have written quite some validations and test for the model and controller. Though I tried to change the names manually but it when I run rake test there are tons of errors. I wonder if rails has a mechanism for such renaming? – ralphxiaoz Mar 03 '15 at 02:28
  • 3
    The mechanism is your editor, edit your code to use `name` instead of `user_name`. – mu is too short Mar 03 '15 at 02:38

2 Answers2

1

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!

patrick
  • 9,837
  • 3
  • 20
  • 27
0

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.

neo
  • 4,078
  • 4
  • 25
  • 41