0

I'm trying to rename the columns in a Rails project, and I generated migration files and bundle exec rake db:migrate.

Procedures are like this.

generate file,

bin/rails g migration RenameUserTwitterNameToName

and write the method with column names,

def change
  rename_column :users, :twitter_name, :name
end

finally execute the rake task.

bundle exec rake db:migrate

Although it works well, I need to modify the controllers related to the columns.

For example, if there is a code like below, I need to change the method find_by_twitter_name to find_by_name

@user = User.find_by_twitter_name(name)

Is there any way to modify the controller like above automatically?

gaaamii
  • 5
  • 3

1 Answers1

0

There is no way to modify the controller automatically, but you can make aliases for attributes, which includes getter, setter, and query methods.

class User < ActiveRecord::Base
    alias_attribute :twitter_name, :name
end

http://api.rubyonrails.org/classes/Module.html#method-i-alias_attribute

scorix
  • 2,436
  • 19
  • 23