mongoid/mongoid — Issue #1794: BSON::DBRef
Author: BRamBoo
Created: 2012-03-06 20:23:45+00:00
State: open

Hey,

When wiring two applications together I ran in the problems with references created with Mongoid. It seems MongoDB uses BSON::DBRef for saving references to other collections, Mongoid however saves just the ObjectID.

I tried implementing the references through DBRef objects but unfortunately Mongoid doesn't support these types.
The way I solved this is:

field :referencedObject, type: Hash

def referenced_object=(o)
self.referencedObject = BSON::DBRef.new{"$id" => o.id, "$ref" => o.collection_name}
end

def referenced_object
referencedObject.find(referencedObject.to_hash["$id"])
end

But as you can Imagine this is far from ideal as I have to do this for every referenced collection in the application. Am I missing something, or is this currently not support by Mongoid?

Thanks,