Created
November 9, 2012 17:50
-
-
Save reconbot/4047130 to your computer and use it in GitHub Desktop.
Self Referential Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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