如何重命名可以自动更改数据库中表名的 ActiveRecord 模型? [英] How to re-name a ActiveRecord Model which can automatically change the table name in DB?

查看:40
本文介绍了如何重命名可以自动更改数据库中表名的 ActiveRecord 模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Active Record 模型car",我想将此模型的名称更改为train"改变内部的功能,那就是改变名称.此外,表名应更改为trains".

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 change the name. Also, the table name should be changed to "trains".

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

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.

有什么好的建议吗?

推荐答案

我会推荐以下内容:

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

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

这篇关于如何重命名可以自动更改数据库中表名的 ActiveRecord 模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆