How to re-name a ActiveRecord Model which can automatically change the table name in DB?
I figured out the following way:
1, generate migration file:
rails generate migration rename_cars_to_trains
edit the created migration file to:
class RenameCarsToTrains < ActiveRecord::Migration def self.up rename_table :cars, :trains end def self.down rename_table :trains, :cars end end
rake db:migrate
After these steps, the table name changed from cars to trains, then, I have to manually change the controller and views names and the associations...
If you have any more efficient way, let me know...
I would recommend the following:
Change manually the Active Record model class into Train
Make migration to change the database table name from cars to trains
Make good search to change references from Car to Train.
If you have constantly the need to change database table names, you might want to reconsider naming the tables more abstact way. Like in this case you could have table called vehicles and have the "type" field specifying the type (for instance: car or train).
I used following steps to rename my model
In sublime text:
- press cmd + shift + find and choose case-sensitive search (see left buttons). It will search word in whole project
- search and replace 'Cars' to 'Trains'
- search and replace 'Car' to 'Train'
- search and replace 'car' to 'train'
- rails generate migration rename_cars_to_trains
manually change following file names
- cars_controller
- car_helper
- Model/ car.rb
- all related files in test folder
change folder name in views: cars to trains