Rails:如何在模型中使用问号?
我们的 mongodb 数据库中有一个名为“失败?”的字段,包括问号。有时,当我访问该字段时,我会得到数据库中的值,有时会得到 null。我希望了解应该发生什么才能进行调试。那么,你可以在模型的字段名称中添加一个问号吗,就像这样:
field :failed?, :type => Boolean, :default => nil
或者我会遇到麻烦吗?假设我已经在数据库中有这个并且必须使用它,我应该如何取出这些字段。
环境:Rails 3.1、JRuby、Mongoid。
We have a field in our mongodb database called "failed?", including the question mark. Sometimes when I access that field, I get the value in the database, and sometimes I get null. I'm looking to understand what should happen a little bit more in order to debug this. So, can you have a question mark in a field name in a model, like so:
field :failed?, :type => Boolean, :default => nil
or am I in for a world of trouble. Assuming I already have this in the database and have to work with it, how should I get the fields out.
Environment: Rails 3.1, JRuby, Mongoid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能是一个 Mongoid 错误,因为字段名称中的问号在 MongoDB 中是有效的。如果我不得不猜测,这可能与 Mongoid 创建的自动
?
存在奇怪的冲突。解决这个问题的最简单方法是尝试通过从 MongoDB 中提取的原始哈希来访问它,您可以使用 model.attributes["failed?"] 来访问它。如果仍然存在问题,则可能是 MongoDB 驱动程序问题。
It's most likely a Mongoid bug as question marks in field names are valid in MongoDB. If I had to take a guess, it could be a weird conflict with the automatic
<field>?
that are created by Mongoid.The easiest way to work around this would be to try accessing it through the raw hash that is pulled out from MongoDB, you can access it with
model.attributes["failed?"]
. If you still have issues, then likely it's a MongoDB driver problem.