Class: Mongoid::Relations::Referenced::ManyToMany

Inherits:
Many show all
Defined in:
lib/mongoid/relations/referenced/many_to_many.rb

Overview

This class defines the behaviour for all relations that are a many-to-many between documents in different collections.

Instance Attribute Summary

Attributes inherited from Proxy

#base, #loaded, #metadata, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Many

#delete_all, #destroy_all, #each, #find, #initialize, #substitute

Methods inherited from Many

#blank?, #exists?, #find_or_create_by, #find_or_initialize_by, #nil?, #respond_to?, #scoped, #serializable_hash

Methods inherited from Proxy

#init, #substitutable

Constructor Details

This class inherits a constructor from Mongoid::Relations::Referenced::Many

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Referenced::Many

Class Method Details

.builder(meta, object, loading = false) ⇒ Builder

Return the builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the builder.

Referenced::ManyToMany.builder(meta, object)

Parameters:

  • meta (Metadata)

    The metadata of the relation.

  • object (Document, Hash)

    A document or attributes to build with.

Returns:

  • (Builder)

    A new builder object.

Since:

  • 2.0.0.rc.1



217
218
219
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 217

def builder(meta, object, loading = false)
  Builders::Referenced::ManyToMany.new(meta, object, loading)
end

.criteria(metadata, object, type = nil) ⇒ Criteria

Create the standard criteria for this relation given the supplied metadata and object.

Examples:

Get the criteria.

Proxy.criteria(meta, object)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • object (Object)

    The object for the criteria.

  • type (Class) (defaults to: nil)

    The criteria class.

Returns:

Since:

  • 2.1.0



234
235
236
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 234

def criteria(, object, type = nil)
  .klass.any_in(:_id => object)
end

.eager_load(metadata, criteria) ⇒ Criteria

Get the criteria that is used to eager load a relation of this type.

Examples:

Get the eager load criteria.

Proxy.eager_load(, criteria)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • criteria (Criteria)

    The criteria being used.

Returns:

  • (Criteria)

    The criteria to eager load the relation.

Raises:

Since:

  • 2.2.0



250
251
252
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 250

def eager_load(, criteria)
  raise Errors::EagerLoad.new(.name)
end

.embedded?false

Returns true if the relation is an embedded one. In this case always false.

Examples:

Is this relation embedded?

Referenced::ManyToMany.embedded?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0.rc.1



263
264
265
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 263

def embedded?
  false
end

.foreign_key_defaultArray

Get the default value for the foreign key.

Examples:

Get the default.

Referenced::ManyToMany.foreign_key_default

Returns:

  • (Array)

    Always an empty array.

Since:

  • 2.0.0.rc.1



275
276
277
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 275

def foreign_key_default
  []
end

.foreign_key_suffixString

Returns the suffix of the foreign key field, either “_id” or “_ids”.

Examples:

Get the suffix for the foreign key.

Referenced::ManyToMany.foreign_key_suffix

Returns:

Since:

  • 2.0.0.rc.1



287
288
289
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 287

def foreign_key_suffix
  "_ids"
end

.macroSymbol

Returns the macro for this relation. Used mostly as a helper in reflection.

Examples:

Get the macro.

Referenced::ManyToMany.macro

Returns:

  • (Symbol)

    :references_and_referenced_in_many



298
299
300
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 298

def macro
  :references_and_referenced_in_many
end

.nested_builder(metadata, attributes, options) ⇒ NestedBuilder

Return the nested builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the nested builder.

Referenced::ManyToMany.builder(attributes, options)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • attributes (Hash)

    The attributes to build with.

  • options (Hash)

    The options for the builder.

Options Hash (options):

  • :allow_destroy (true, false)

    Can documents be deleted?

  • :limit (Integer)

    Max number of documents to create at once.

  • :reject_if (Proc, Symbol)

    If documents match this option then they are ignored.

  • :update_only (true, false)

    Only existing documents can be modified.

Returns:

  • (NestedBuilder)

    A newly instantiated nested builder object.

Since:

  • 2.0.0.rc.1



324
325
326
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 324

def nested_builder(, attributes, options)
  Builders::NestedAttributes::Many.new(, attributes, options)
end

.path(document) ⇒ Root

Get the path calculator for the supplied document.

Examples:

Get the path calculator.

Proxy.path(document)

Parameters:

  • document (Document)

    The document to calculate on.

Returns:

  • (Root)

    The root atomic path calculator.

Since:

  • 2.1.0



338
339
340
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 338

def path(document)
  Mongoid::Atomic::Paths::Root.new(document)
end

.stores_foreign_key?true

Tells the caller if this relation is one that stores the foreign key on its own objects.

Examples:

Does this relation store a foreign key?

Referenced::Many.stores_foreign_key?

Returns:

  • (true)

    Always true.

Since:

  • 2.0.0.rc.1



351
352
353
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 351

def stores_foreign_key?
  true
end

.valid_optionsArray<Symbol>

Get the valid options allowed with this relation.

Examples:

Get the valid options.

Relation.valid_options

Returns:

Since:

  • 2.1.0



363
364
365
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 363

def valid_options
  [ :autosave, :dependent, :foreign_key, :index, :order ]
end

.validation_defaulttrue, false

Get the default validation setting for the relation. Determines if by default a validates associated will occur.

Examples:

Get the validation default.

Proxy.validation_default

Returns:

  • (true, false)

    The validation default.

Since:

  • 2.1.9



376
377
378
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 376

def validation_default
  true
end

Instance Method Details

#<<(*args) ⇒ Array<Document> Also known as: concat, push

Appends a document or array of documents to the relation. Will set the parent and update the index in the process.

Examples:

Append a document.

person.posts << post

Push a document.

person.posts.push(post)

Concat with other documents.

person.posts.concat([ post_one, post_two ])

Parameters:

Returns:

Since:

  • 2.0.0.beta.1



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 27

def <<(*args)
  batched do
    [].tap do |ids|
      args.flatten.each do |doc|
        next unless doc
        append(doc)
        if persistable? || creating?
          ids.push(doc.id)
          doc.save
        else
          base.send(.foreign_key).push(doc.id)
          base.synced[.foreign_key] = false
        end
      end
      if persistable? || creating?
        base.push_all(.foreign_key, ids)
        base.synced[.foreign_key] = false
      end
    end
  end
end

#build(attributes = {}, type = nil) ⇒ Document Also known as: new

Build a new document from the attributes and append it to this relation without saving.

Examples:

Build a new document on the relation.

person.posts.build(:title => "A new post")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes of the new document.

  • type (Class) (defaults to: nil)

    The optional subclass to build.

Returns:

Since:

  • 2.0.0.beta.1



63
64
65
66
67
68
69
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 63

def build(attributes = {}, type = nil)
  Factory.build(type || klass, attributes).tap do |doc|
    base.send(.foreign_key).push(doc.id)
    append(doc)
    yield(doc) if block_given?
  end
end

#create(attributes = nil, type = nil, &block) ⇒ Document

Creates a new document on the references many relation. This will save the document if the parent has been persisted.

Examples:

Create and save the new document.

person.posts.create(:text => "Testing")

Parameters:

  • attributes (Hash) (defaults to: nil)

    The attributes to create with.

  • type (Class) (defaults to: nil)

    The optional type of document to create.

Returns:

  • (Document)

    The newly created document.

Since:

  • 2.0.0.beta.1



84
85
86
87
88
89
90
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 84

def create(attributes = nil, type = nil, &block)
  super.tap do |doc|
    base.send(.foreign_key).delete_one(doc.id)
    base.push(.foreign_key, doc.id)
    base.synced[.foreign_key] = false
  end
end

#create!(attributes = nil, type = nil, &block) ⇒ Document

Creates a new document on the references many relation. This will save the document if the parent has been persisted and will raise an error if validation fails.

Examples:

Create and save the new document.

person.posts.create!(:text => "Testing")

Parameters:

  • attributes (Hash) (defaults to: nil)

    The attributes to create with.

  • type (Class) (defaults to: nil)

    The optional type of document to create.

Returns:

  • (Document)

    The newly created document.

Raises:

Since:

  • 2.0.0.beta.1



107
108
109
110
111
112
113
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 107

def create!(attributes = nil, type = nil, &block)
  super.tap do |doc|
    base.send(.foreign_key).delete_one(doc.id)
    base.push(.foreign_key, doc.id)
    base.synced[.foreign_key] = false
  end
end

#delete(document) ⇒ Document

Delete the document from the relation. This will set the foreign key on the document to nil. If the dependent options on the relation are :delete or :destroy the appropriate removal will occur.

Examples:

Delete the document.

person.posts.delete(post)

Parameters:

  • document (Document)

    The document to remove.

Returns:

Since:

  • 2.1.0



127
128
129
130
131
132
133
134
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 127

def delete(document)
  super.tap do |doc|
    if doc && persistable?
      base.pull(.foreign_key, doc.id)
      base.synced[.foreign_key] = false
    end
  end
end

#nullifyObject Also known as: nullify_all, clear, purge

Removes all associations between the base document and the target documents by deleting the foreign keys and the references, orphaning the target documents in the process.

Examples:

Nullify the relation.

person.preferences.nullify

Since:

  • 2.0.0.rc.1



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mongoid/relations/referenced/many_to_many.rb', line 144

def nullify
  unless .forced_nil_inverse?
    criteria.pull(.inverse_foreign_key, base.id)
  end
  if persistable?
    base.set(
      .foreign_key,
      base.send(.foreign_key).clear
    )
  end
  target.clear do |doc|
    unbind_one(doc)
  end
end