Rails:如何在模型中使用问号?

发布于 2024-12-25 14:23:04 字数 292 浏览 1 评论 0原文

我们的 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 技术交流群。

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

发布评论

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

评论(1

短暂陪伴 2025-01-01 14:23:04

这很可能是一个 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.

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