Ruby-on-rails 如何重命名可以自动更改数据库中表名的 ActiveRecord 模型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4826879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:13:39  来源:igfitidea点击:

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

ruby-on-railsruby-on-rails-3

提问by Mellon

I have a Active Record model "car", I would like to change the name of this model to "train" withoutchanging functionalities inside, that's onlychange the name. Also, the table name should be changed to "trains".

我有一个 Active Record 模型“汽车”,我想将此模型的名称更改为“火车”而不更改内部功能,这只是更改名称。此外,表名应更改为“trains”。

Is there any rails command can do that at onece? Or I have to manually change the name in side the class or migration? If I have to change manually, it's gonna be complicated, because I have to also change other models which have associations to my "car" model.

有没有任何 rails 命令可以一次做到这一点?或者我必须在类或迁移中手动更改名称?如果我必须手动更改,那会很复杂,因为我还必须更改与我的“汽车”模型有关联的其他模型。

Any good suggestions?

有什么好的建议吗?

回答by Mellon

I figured out the following way:

我想出了以下方法:

1, generate migration file:

1、生成迁移文件:

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

  1. 将创建的迁移文件编辑为:

    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...

如果您有更有效的方法,请告诉我...

回答by hade

I would recommend the following:

我会推荐以下内容:

  1. Change manually the Active Record model class into Train

  2. Make migration to change the database table name from cars to trains

  3. Make good search to change references from Car to Train.

  1. 手动将 Active Record 模型类更改为 Train

  2. 进行迁移以将数据库表名称从汽车更改为火车

  3. 进行良好的搜索以将引用从汽车更改为火车。

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).

如果您经常需要更改数据库表名,您可能需要重新考虑以更抽象的方式命名表。就像在这种情况下一样,您可以拥有名为车辆的表,并具有指定类型的“类型”字段(例如:汽车或火车)。

回答by Omer Aslam

I used following steps to rename my model

我使用以下步骤重命名我的模型

In sublime text:

在崇高的文字中:

  1. press cmd + shift + find and choose case-sensitive search (see left buttons). It will search word in whole project
  2. search and replace 'Cars' to 'Trains'
  3. search and replace 'Car' to 'Train'
  4. search and replace 'car' to 'train'
  5. rails generate migration rename_cars_to_trains
  6. manually change following file names

    • cars_controller
    • car_helper
    • Model/ car.rb
    • all related files in test folder
  7. change folder name in views: cars to trains

  1. 按 cmd + shift + find 并选择区分大小写的搜索(见左侧按钮)。它将在整个项目中搜索单词
  2. 搜索并将“汽车”替换为“火车”
  3. 搜索并将“汽车”替换为“火车”
  4. 搜索并将“汽车”替换为“火车”
  5. rails 生成迁移 rename_cars_to_trains
  6. 手动更改以下文件名

    • 汽车控制器
    • 汽车助手
    • 型号/汽车.rb
    • 测试文件夹中的所有相关文件
  7. 在视图中更改文件夹名称:汽车到火车

回答by JuJoDi

If you're using RubyMine, go into the model, right click the class name > refactor and change the name. RubyMine will refactor everything and create a new migration for the database as well.

如果您使用的是 RubyMine,请进入模型,右键单击类名 > 重构并更改名称。RubyMine 将重构所有内容并为数据库创建一个新的迁移。