Reputation: 2130
I am getting confused with mongomapper + sorting.
I have a test blog with data with posts like this:
http://www.mongodb.org/display/DOCS/Schema+Design
Now, I would like to show just comments sorted by time asc for each blog entry.
If I use where, I get Plucky, when I use all() I get Array...how does this work?
Upvotes: 0
Views: 508
Reputation: 19145
The answer you linked to is sorting your result set in memory, which is expensive and likely going to cause errors if you have more comments than you can display in one batch.
The right way to sort this is using plucky's sort() method on the query, by created_at, descending (newest first):
post.comments.sort(:created_at).each {|comment| do_awesome_stuff(comment) }
(assuming you have a Post model that has_many comments and you're using the built-in created_at field on the comments for time.
Mongomapper uses a chaining query/filter model like ActiveRecord's scopes, which is why where() returns a chain-able Plucky query, as does sort(). You can then convert that into an array of results or chain more sorts/filters. You can also use enumerators like each() directly on the plucky query.
The code in the example above is doing the following chained calls:
Upvotes: 2