2015-12-23 67 views
2

當我嘗試在mongo ruby​​驅動程序中使用DBRef時(創建一個新的DBRef對象並在文檔中包含我插入的集合),我得到這個錯誤,我不能做的頭,也沒有尾巴:在mongo ruby​​驅動程序中使用DBRef:(undefined method`bson_type'for#<Mongo :: DBRef:0x0056466ed55e48>)

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' 

這裏是有問題的代碼:39

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 

線指的是倒數第二在代碼片段中。

回答

1

Ruby Mongo驅動程序(我正在使用v2.2.0)未在Mongo::DBRef類中定義bson_type方法。通過將數據庫引用指定爲擴展JSON(link),我能夠解決此特定限制。將您的grant_document散列定義爲:

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