任何避免使用“类型”一词的理由都可以。在 Rails 中使用 Mongoid?

发布于 2025-01-05 16:20:11 字数 408 浏览 2 评论 0原文

我知道 type 是一个带有 魔法字段名称 ActiveRecord 用于单表继承。但是 :type 不包含在 Mongoid.pressive_fields 中。有什么原因我不能将其用作字段名称吗? Upload.upload_type 感觉很笨拙,我更喜欢使用 Upload.type

此外,classcategoryvariety 似乎并不是很好的选择。

I know type is a magic field name with ActiveRecord for use in Single Table Inheritance. But :type is not included in Mongoid.destructive_fields. Is there any reason I can't use it as a field name? Upload.upload_type feels so ackward and I'd much prefer to use Upload.type.

Furthermore class, category, and variety don't seem like good alternatives.

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

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

发布评论

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

评论(3

萌能量女王 2025-01-12 16:20:11

到目前为止我正在使用它没有任何问题。因此,除非我在路上遇到任何问题,否则我会回答“不”,没有理由避免使用“类型”这个词。

I am using it without any problems so far. So unless I run into any issues down the road I'm going to answer no, there's no reason to avoid using the word "type".

梦里南柯 2025-01-12 16:20:11

在 Mongoid 中,定义了一个属性 _type 来帮助 STI。所以最好也避免使用它。

In Mongoid an attribute _type is define to help in the STI too. So it's better to avoid using it too.

笑看君怀她人 2025-01-12 16:20:11

您是正确的,“_type”用于 STI,因此“type”在技术上是一个有效的属性名称。它会很好地适合你的情况。不过,我会谨慎使用这些“特殊名称”。如果您切换到不同的数据库,您可能会遇到问题,但如果您可以控制它并且知道您将使用 MongoDB,那么就没有充分的理由不能使用“类型”字段。

我个人不会使用许多数据库或框架使用的任何特殊单词,因为它只会让其他人不那么困惑。但这只是个人喜好;没有技术原因。

You are correct that "_type" is used for STI and thus "type" is technically a valid attribute name. It will work for your situation just fine. I would just be cautious using those "special names", however. If you ever switch over to a different database you will probably run into problems, but if you have control over that and know that you'll be using MongoDB, then there isn't a really good reason why you can't use the "type" field.

I personally would not use any of the special words that many databases or frameworks use, as it just makes it less-confusing for others. But that is just a personal preference; there's no technical reason.

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