ruby-on-rails - 在 mongo ruby​​ 驱动程序中使用 DBRef:(#<Mongo::DBRef:0x0056466ed55e48> 的未定义方法 `bson_type')

标签 ruby-on-rails ruby mongodb mongodb-ruby

当我尝试在 mongo ruby​​ 驱动程序中使用 DBRef 时(创建一个新的 DBRef 对象并包含在我要插入到集合中的文档中),我收到此错误,我无法弄清楚:

NoMethodError (undefined method `bson_type' for #<Mongo::DBRef:0x0056466ed55e48>):
  app/controllers/payment_notifications_controller.rb:43:in `block in create'
  app/controllers/payment_notifications_controller.rb:19:in `create'

这是有问题的代码:

user_mongo = Urgent::Application.config.mongo_client[:user].find(uuid: order.identity.uuid)
if user_mongo
  grant_document = { :target => Mongo::DBRef.new("user", user_mongo.first["_id"]), :role => order_item.expirable.backend_id, :created => Time.now, :store_item_id => order_item.id, :store_order_id => order.id }

  if expires
    grant_document[:expires] = expires
  end

  Urgent::Application.config.mongo_client[:grant].insert_one(grant_document)
end

第 39 行指的是代码片段中倒数第二行。

最佳答案

Ruby Mongo 驱动程序(我使用的是 v2.2.0)没有在 Mongo::DBRef 类上定义 bson_type 方法。我能够通过将数据库引用指定为扩展 JSON ( link ) 来解决此特定限制。将您的 grant_document 哈希定义为:

grant_document = { :target => { "$ref" => "user", "$id" => user_mongo.first["_id"] }, :role => ... }

关于ruby-on-rails - 在 mongo ruby​​ 驱动程序中使用 DBRef:(#<Mongo::DBRef:0x0056466ed55e48> 的未定义方法 `bson_type'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34429292/

相关文章:

javascript - 发布到 map 显示的限制

ruby-on-rails - 控制 Rails 中使用的 ruby​​ 版本

Ruby 线程和互斥量

mongodb - 当使用带 _id 的复合分片键时,MongoDB 是否确保唯一的 _id 字段值

mongodb - 食物记录的数据库模式

javascript - 获取 $group by 之后的最新文档

javascript - font-awesome 图标链接到一个 URL,需要想法和解决方案

ruby-on-rails - 使用 rmagick 调整 ruby​​ on Rails 中的图像大小而不损失质量

ruby-on-rails - 为什么 "rails s"在 app 目录中不起作用?

区间上的 JavaScript 函数不稳定