mongoid/mongoid — Issue #1393: Finders return a Criteria object
Author: eimermusic
Created: 2011-11-01 10:14:51+00:00
State: open

I noticed a changed behaviour which is not documented and which can bite you in some cases.

Finders, which should return an array of results when doing things like:

Person.all
Person.all(conditions: { first_name: /^dur/i, "addresses.city" => "Berlin" })

Have started returning an instance of Mongoid::Criteria. If I just each through it all is well but in one place I am serializing the found documents and this now gives me a nice exception.... saying I can't serialize an instance if Mongoid::Criteria... (and not an array of Person objects).

The docs still read as before so I am not sure if this is an intended change or a bug? I have run it on both 2.2.0 and the latest gem which today was 2.2.4. Since this is something that only bites me in a single rarely used (and poorly tested) corner of an app I have nog good idea of when this changed.

Oh, one question... how the H... do I now end a criteria chain and perform the search? I have always relied on first or all for that.

Thanks.