Skip to content

Instantly share code, notes, and snippets.

@lukelex
Created August 21, 2012 17:37
Show Gist options
  • Save lukelex/3417664 to your computer and use it in GitHub Desktop.
Save lukelex/3417664 to your computer and use it in GitHub Desktop.
Getting the relational hierarchy of a mongo embedded document
module EmbeddedSearch
def self.included(base) # :nodoc:
base.extend ClassMethods
end
module ClassMethods
def hierarchy
value = ""
klass = self
while klass.embedded?
klass = klass.relations.select do |k,v|
v.relation == Mongoid::Relations::Embedded::In
end.first.first
value += klass + "."
klass = klass.classify.constantize
value += 's' if klass.embedded?
end
value += self.to_s.downcase + "s"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment