仅访问 ruby 中 BSON::ObjectId 的值
我正在开发一个由 Mongodb 支持的 ruby/rails 应用程序(使用 Mongoid)。在 Rails 应用程序的上下文中,一切都工作得完美无缺,但我们还访问 Rails 环境之外的对象,在这种情况下,我无法将对象的 id 以除散列格式之外的任何形式返回:
{"$oid"=>"4e0005b78ba4db213500001f"}
我想通了我之所以看到这一点,是因为我返回的值不仅仅是 id 字符串,而且是 BSON::ObjectId 类型。除了要求 Rails 环境之外,我还尝试在执行这项工作的文件中显式要求 bson:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require "bson"
我需要简单地以包含 id 的字符串形式获取值,这似乎是其他地方的默认值,但在这种情况下。奇怪的是,这种行为只发生在我的开发环境中(我团队中的其他人没有这个问题)。
必要的免责声明是,我是 Mongodb 的新手,因此可能会遗漏一些真正明显的东西。
谢谢!
I'm working on a ruby/rails app backed by Mongodb (using Mongoid). Within the context of the Rails application everything works flawlessly but we're also accessing objects outside of the Rails environment, where I'm having trouble getting the id of an object to return as anything but a hash in the format:
{"$oid"=>"4e0005b78ba4db213500001f"}
I've figured out that I'm seeing because I'm getting back a value that's not just an id string but rather of the type BSON::ObjectId. In addition to requiring the rails environment I've also tried requiring bson explicitly in the file that's doing this work:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require "bson"
I need to get the value simply as a string containing the id, which seems to be the default everywhere else but in this one case. Oddly this behavior only occurs in my dev environment (the rest of the guys on my team don't have this problem).
Requisite disclaimer that I'm new to Mongodb so could be missing something truly obvious.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试在对象上调用 to_s 。在 irb 中:
如果这不起作用,您可以发布您的环境.rb 吗?
You could try calling to_s on the object. In irb:
If that doesn't work, can you post your environment.rb?