Rails 迁移的命名约定

发布于 2024-08-05 02:04:29 字数 266 浏览 3 评论 0原文

Rails 迁移是否有最佳实践命名约定,特别是在编辑模型时?

例如,如果我要向 Foo 模型添加一列 bar ,我应该将其命名为 edit_foo 还是 add_bar_to_foo

假设如果我正在编辑多个模型,那么我应该创建多个迁移,但是如果我对单个模型进行多次修改,我是否将其命名为 add_bar_remove_x_edit_y_to_foo 呢?

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

e.g. if I'm adding a column bar to the Foo model, should I name it edit_foo or add_bar_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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如日中天 2024-08-12 02:04:29

我同意之前的海报。命名应注重可读性。但还要记住,您不能(也不应该)有两个同名的迁移。

因此,像 edit_foo_model 这样的通用名称通常不是一个好主意(因为,当您想向该模型添加更多列时会发生什么),那么最好将列分组到目的中,如update_foo_for_bar_support。您通常可以跳过添加模型,因为每个人都知道迁移确实会处理模型,因此无需在名称中提及(即 update_foo 而不是 update_foo_model)。

另外,我通常所做的是将不同的更改分开。因此,如果模型中有多个不同的更改,我会将它们分成不同的迁移文件,例如一个用于添加列,一个用于删除列。

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.

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.

℡Ms空城旧梦 2024-08-12 02:04:29

我会将多个架构更改拆分为多个迁移,以便您可以轻松命名单个迁移!

I would split up multiple schema changes into multiple migrations so that you can easily name the single migrations!

飘落散花 2024-08-12 02:04:29

重点是可读性——快速找出迁移负责的内容。如果你在名称中写了太多的“数据”,就会变得更难扫描,你就会搬起石头砸自己的脚。

所以..如果是1-2个变化,就写在名称中,如果变化太多,就写update_foo_model(或edit_foo_model)

The point is readability- to find out fast what the migration is responsible for.. if you write too much "data" in the name, it makes it harder to scan and you shoot yourself in the foot.

So.. if it's 1-2 changes, write it in the name, if there are too many changes, write update_foo_model (or edit_foo_model)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文