-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
None
Just started using Mongoid (and mongo in general), after finding PG hstore to be insufficient for my needs.
In moving over I ran into some roadblocks concerning finding embedded objects.
It would be extremely helpful to have some finders for documents which are embedded. Here is an example I came up with, probably not the most elegant solution, and I'm not sure what module these would need to go into, but heres my idea:
def self.find_by_embedded_id(embedded_object, id) id = Moped::BSON::ObjectId(id) if id.is_a? String embedded_object = embedded_object.to_s.pluralize.to_sym self.where("#{embedded_object}._id" => id).first end def self.find_embedded(embedded_object, id) embedded_object = embedded_object.to_s.pluralize.to_sym self.find_by_embedded_id(embedded_object, id).try(:send, embedded_object).try(:find, id).try(:first) end def self.find_embedded_in(embedded_in_object, id) id = Moped::BSON::ObjectId(id) if id.is_a? String embedded_in_object = embedded_in_object.to_s.pluralize.to_sym embedded_in_class = embedded_in_object.to_s.classify.constantize klass = self.to_s.underscore.pluralize.to_sym embedded_in_class.find_by_embedded_id(klass, id) end
Or even possibly doing something where self.find will perform an embedded find on any class that the the self class is embedded in until it gets a non nil result.