仅访问 ruby​​ 中 BSON::ObjectId 的值

发布于 2024-11-16 18:23:58 字数 583 浏览 3 评论 0原文

我正在开发一个由 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 技术交流群。

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

发布评论

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

评论(1

从来不烧饼 2024-11-23 18:23:58

您可以尝试在对象上调用 to_s 。在 irb 中:

ruby-1.9.2-p180 > p = Project.last #=> #<Project _id: 4e00e77d399a46759d000002, _type: nil, version: 1, created_at: 2011-06-21 18:48:34 UTC, updated_at: 2011-06-21 18:48:34 UTC, name: "Testing MongoDB", client_id: 3, client_name: nil, group_id: 35, requestor_id: 14, requestor_name: "Test Client User", requestor_phone: "", creator_id: 2, creator_name: "Some Guy", manager_id: 23, manager_name: "Some Other Guy", manager_phone: "", manager_email: "", active: true, status: "open", default_hourly_cost: "0.0", default_hourly_charge: "0.0", default_material_markup: "0.35", add_email_internal: "", add_email_client: "", client_po_number: "", client_ticket_number: "", date_requested: nil, date_requested(1i): "2011", date_requested(2i): "6", date_requested(3i): "21">
ruby-1.9.2-p180 > p.id.to_s #=> "4e00e77d399a46759d000002"

如果这不起作用,您可以发布您的环境.rb 吗?

You could try calling to_s on the object. In irb:

ruby-1.9.2-p180 > p = Project.last #=> #<Project _id: 4e00e77d399a46759d000002, _type: nil, version: 1, created_at: 2011-06-21 18:48:34 UTC, updated_at: 2011-06-21 18:48:34 UTC, name: "Testing MongoDB", client_id: 3, client_name: nil, group_id: 35, requestor_id: 14, requestor_name: "Test Client User", requestor_phone: "", creator_id: 2, creator_name: "Some Guy", manager_id: 23, manager_name: "Some Other Guy", manager_phone: "", manager_email: "", active: true, status: "open", default_hourly_cost: "0.0", default_hourly_charge: "0.0", default_material_markup: "0.35", add_email_internal: "", add_email_client: "", client_po_number: "", client_ticket_number: "", date_requested: nil, date_requested(1i): "2011", date_requested(2i): "6", date_requested(3i): "21">
ruby-1.9.2-p180 > p.id.to_s #=> "4e00e77d399a46759d000002"

If that doesn't work, can you post your environment.rb?

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