Module: MongoDoc::References

Defined in:
lib/mongo_doc/references.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dereference(db_ref) ⇒ Object

Dereference a DBRef and return the Object



5
6
7
# File 'lib/mongo_doc/references.rb', line 5

def self.dereference(db_ref)
  MongoDoc::Collection.new(db_ref.namespace).find_one(db_ref.object_id)
end

Instance Method Details

#references(*args) ⇒ Object

Declare a reference to another Document. The reference can either be an ObjectId reference, or a BSON::DBRef

Use an ObjectId reference when you have a simple reference or will be referencing a single polymorphic collection. Example:

references :address references :address, :as => :work_address+

  • classname

    name of Document type as an underscore symbol or string

  • options

    :as specifies the name of the attribute, defaults to

classname

Use a BSON::DBRef when you need a reference to multiple collections. Example:

references :as_ref => :work_address

  • options

    :as_ref name of the attribute



28
29
30
31
32
33
34
35
36
37
# File 'lib/mongo_doc/references.rb', line 28

def references(*args)
  options = args.extract_options!

  if options.has_key?(:as_ref)
    references_by_dbref(options[:as_ref])
  else
    klass = args[0].to_s.camelize
    references_by_id(klass, options[:as] || klass.to_s.demodulize.underscore)
  end
end