Skip to content

Instantly share code, notes, and snippets.

@reconbot
Created November 9, 2012 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reconbot/4047130 to your computer and use it in GitHub Desktop.
Save reconbot/4047130 to your computer and use it in GitHub Desktop.
Self Referential Model
class Entity
include MongoMapper::Document
safe
before_destroy :remove_relations
key :relation_ids, Array
key :name, String
many :relations, :in => :relation_ids, :class => Entity
def related
relations + Entity.where(:relation_ids => _id).all
end
def self.create_indexes
ensure_index :relation_ids
end
private
def remove_relations
Entity.pull({},:relation_ids => id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment