Mongoid 3.0.0: Query by "id" field, not "_id"

142 views Asked by At

Is there an idiomatic way in Mongoid 3.0.0 to query a collection of documents using a non-unique field named id which is not the same as _id?

#<BltBasket _id: 52a16cd36cf462b442871709, id: 9334, ...>

Thanks!

1

There are 1 answers

1
xlembouras On BEST ANSWER

For me one good thing about mongoid is that you can define aliases.

So for your example I would see it as

field :_id, as: :uid,          type: String
field :id,  as: :a_funky_name, type: String

which will provide you with a

BltBasket.find_by( a_funky_name: :foo )

Excuse me if I understood your question the wrong way :-)