Is there some sort of find_by_sql equivalent for mongoid, where you pass a mongo query and it materializes Mongoid::Document s from the results?
+2
A:
Mongoid wraps the Collection object to return objects of the proper class.
So, if User is a Mongoid model:
cursor = User.collection.find({}, {}) # Just like the Ruby driver...
records = cursor.to_a # An array of User objects
Edit to add: It actually wraps Mongo's Cursor class too. See here:
def each
@cursor.each do |document|
yield Mongoid::Factory.build(@klass, document)
end
end
PreciousBodilyFluids
2010-09-16 20:28:22
that is absolutely amazing! do you know how the mongo driver knows to materialize my mongoid class?
Matt Briggs
2010-09-16 20:48:30