Skip to content

Instantly share code, notes, and snippets.

@benzittlau
Created May 4, 2011 21:45
Show Gist options
  • Save benzittlau/956104 to your computer and use it in GitHub Desktop.
Save benzittlau/956104 to your computer and use it in GitHub Desktop.
Why you have to be careful using versions in Mongoid
Mongoid/Hierarchy
# Get all child +Documents+ to this +Document+, going n levels deep if
# necessary. This is used when calling update persistence operations from
# the root document, where changes in the entire tree need to be
# determined. Note that persistence from the embedded documents will
# always be preferred, since they are optimized calls... This operation
# can get expensive in domains with large hierarchies.
#
# @example Get all the document's children.
# person._children
#
# @return [ Array<Document> ] All child documents in the hierarchy.
def _children
relations.inject([]) do |children, (name, metadata)|
children.tap do |kids|
if metadata.embedded? && name != "versions"
child = send(name)
child.to_a.each do |doc|
kids.push(doc).concat(doc._children)
end unless child.blank?
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment