There are a lot of factors that would affect this. So I'll briefly explain
the behavior of first/last in Mongoid and MongoDB.
First of all, MongoDB does not guarantee any exact order in a collection,
so issuing a query with limit -1 (essentially a "find one") is never
guaranteed to return you the same document every time.
With Mongoid, we wanted some sort of consistency here, so this is how we
implemented #first and #last:
For #first:
1. If no sorting options exists on the criteria, or no default scope with
sorting criteria exists, Mongoid adds an ascending sort to the query by _id.
2. If sorting options exist on the criteria, or a default scope on the
model exists with sort options, Mongoid applies no extra sorting.
For #last:
1. If no sorting options exists on the criteria, or no default scope with
sorting criteria exists, Mongoid adds a descending sort to the query by _id.
2. If sorting options exist on the criteria, or a default scope on the
model exists with sort options, Mongoid reverses the sort order.
So what I would suspect is that you've got some sorting applied, or check
the ids in the collection.
2013/1/8 Learner <
[email protected]>
I've created a new User but when I run User.last it doesn't show the most
recently created User, but a user i've created a few days back. In the
past its always been the last item in the collection. Let me know if I can
provide more info. Thanks
On Monday, January 7, 2013 4:16:27 AM UTC-8, Arthur Neves wrote:
Can u post an example do clarify your question?
Thanks
--
Arthur Neves
On Monday, 7 January, 2013 at 12:42 AM, Learner wrote:
Is this a new change? I started to notice this in the last week. Or, is
there something I totally misconfigured?
Thanks