rs-pro / mongoid-elasticsearch

DEPRECATED - Consider using SearchKick https://github.com/ankane/searchkick
MIT License
61 stars 23 forks source link

Association $oid is not restored right when using :model wrapper #1

Closed Antti closed 10 years ago

Antti commented 10 years ago

When you use :model wrapper, and it loads object attributes from ES to Mongoid model, it sets your association's id as {"$oid" => BSON::ObjectId()}, where it should just be BSON::ObjectId().

my_object.inspect =>
# ... my_relation_id: {"$oid"=>BSON::ObjectId('529f85db616e640d58dcc300')}  ...

So when you call your relation,you'll get an exception:

my_object.my_relation
"invalid operator: $oid"

I think it's more Mongoid problem: when you dump BSON::ObjectId you get {"$oid" => "abc012345"}, and you can not load it back as BSON::ObjectID using JSON.parse. So we should do some workarounds, for example check if restored object is a hash with $oid and then intitalize BSON::ObjectId object for it.

Some related info https://github.com/mongoid/mongoid/pull/2947