命名约定Rails迁移 [英] Naming conventions for Rails migrations

查看:105
本文介绍了命名约定Rails迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有Rails迁移最佳实践命名约定,在编辑模式尤其是当?

Is there a best practice naming convention for Rails migrations, particularly when editing a model?

例如。如果我添加一列模式,我应该将其命名为 edit_foo add_bar_to_foo

e.g. if I'm adding a column bar to the Foo model, should I name it edit_foo or add_bar_to_foo

我假设,如果我编辑复式车型那么我应该创建多个迁移,但如果我进行多次修改,一个单一的模式,我将其命名为 add_bar_remove_x_edit_y_to_foo

I'm assuming if I'm editing mutliple models then I should create multiple migrations, but what if I'm making multiple modifications to a single model, do I name it add_bar_remove_x_edit_y_to_foo?

推荐答案

我同意previous海报。命名应注重可读性。但是,也请记住,你不能(也不应该)有两个迁移具有相同的名称。

I agree with the previous poster. The naming should focus on readability. But also keep in mind that you can't (nor should) have two migrations with the same name.

因此​​,像 edit_foo_model 通用名称通常不是一个好主意(因为,当你想添加更多的列到这种模式会发生什么),那么这将是更好组列进的目的是什么,如 update_foo_for_bar_support 。通常你可以跳过添加模式,因为,好了,大家都知道,迁移做处理模式,所以没有必要提及的是,在名称(即 update_foo 而不是 update_foo_model )。

So, general names like edit_foo_model is generally not a good idea (since, what happens when you want to add more columns to that model), then it would be better to group the columns into what the purpose is, like update_foo_for_bar_support. You can usually skip adding model since, well, everyone knows that migrations do handle models, so there is no need to mention that in the name (that is, update_foo instead of update_foo_model).

另外,我通常做的是保持分开不同的变化。所以,如果有一个在模型中的多个不同的变化,身份证将其分别放入不同的迁移文件,一个用于添加列,一个用于例如删除列

Also, what I usually do is to keep different changes separated. So, if there are multiple different changes in a model, I'd separate them into different migration files, one for adding columns and one for removing columns for instance.

这篇关于命名约定Rails迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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