How to re-name a ActiveRecord Model which can automatically change the table name in DB?

后端 未结 4 1381
野趣味
野趣味 2021-02-04 05:21

I have a Active Record model \"car\", I would like to change the name of this model to \"train\" without changing functionalities inside, that\'s only

4条回答
  •  遇见更好的自我
    2021-02-04 05:44

    I figured out the following way:

    1, generate migration file:

    rails generate migration rename_cars_to_trains
    
    1. 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
      
    2. 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...

提交回复
热议问题