在ruby on rails更改表名的最佳实践?



我有一个模型,我想移动到一个命名空间,并与此同时重命名表,以包括命名空间名称。这样做的最佳实践是什么?

我在项目中使用强迁移,当尝试只做rename_table:[old_table_name],:[new_table_name]时,它不会允许它,而是给我这个建议:

=== Dangerous operation detected #strong_migrations ===
Renaming a table that's in use will cause errors
in your application. A safer approach is to:
1. Create a new table. Don't forget to recreate indexes from the old table
2. Write to both tables
3. Backfill data from the old table to new table
4. Move reads from the old table to the new table
5. Stop writing to the old table
6. Drop the old table

还有别的办法吗?这个表本身不是很常用,只有大约400条记录。

您可以在模型中添加名称空间,而不是在迁移中。我想这篇文章会对你有帮助https://blog.bullettrain.co/rails-model-namespacing/

最新更新