任何避免使用“类型”一词的理由都可以。在 Rails 中使用 Mongoid?
我知道 type
是一个带有 的 魔法字段名称 ActiveRecord
用于单表继承。但是 :type
不包含在 Mongoid.pressive_fields
中。有什么原因我不能将其用作字段名称吗? Upload.upload_type
感觉很笨拙,我更喜欢使用 Upload.type
。
此外,class
、category
和 variety
似乎并不是很好的选择。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
到目前为止我正在使用它没有任何问题。因此,除非我在路上遇到任何问题,否则我会回答“不”,没有理由避免使用“类型”这个词。
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".
在 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.您是正确的,“_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.