Class: Mongoid::Relations::Many

Inherits:
Proxy
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mongoid/relations/many.rb

Overview

This is the superclass for all many to one and many to many relation proxies.

Direct Known Subclasses

Embedded::Many, Referenced::Many

Instance Attribute Summary

Attributes inherited from Proxy

#__metadata, #base, #target

Instance Method Summary collapse

Methods inherited from Proxy

apply_ordering, #extend_proxies, #init, #klass, #reset_unloaded, #substitutable, #with

Methods included from Marshalable

#marshal_dump, #marshal_load

Dynamic Method Handling

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

Instance Method Details

#blank?true, false

Is the relation empty?

Examples:

Is the relation empty??

person.addresses.blank?

Returns:

  • (true, false)

    If the relation is empty or not.

Since:

  • 2.1.0



21
22
23
# File 'lib/mongoid/relations/many.rb', line 21

def blank?
  size == 0
end

#create(attributes = nil, options = {}, type = nil) ⇒ Document #create(attributes = nil, type = nil) ⇒ 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")

Overloads:

  • #create(attributes = nil, options = {}, type = nil) ⇒ Document

    Parameters:

    • attributes (Hash) (defaults to: nil)

      The attributes to create with.

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

      The scoped assignment options.

    • type (Class) (defaults to: nil)

      The optional type of document to create.

  • #create(attributes = nil, type = nil) ⇒ Document

    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



43
44
45
46
47
48
49
50
51
# File 'lib/mongoid/relations/many.rb', line 43

def create(attributes = nil, type = nil, &block)
  if attributes.is_a?(::Array)
    attributes.map { |attrs| create(attrs, type, &block) }
  else
    doc = build(attributes, type, &block)
    base.persisted? ? doc.save : raise_unsaved(doc)
    doc
  end
end

#create!(attributes = nil, options = {}, type = nil) ⇒ Document #create!(attributes = nil, type = nil) ⇒ 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")

Overloads:

  • #create!(attributes = nil, options = {}, type = nil) ⇒ Document

    Parameters:

    • attributes (Hash) (defaults to: nil)

      The attributes to create with.

    • type (Class) (defaults to: nil)

      The optional type of document to create.

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

    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



73
74
75
76
77
78
79
80
81
# File 'lib/mongoid/relations/many.rb', line 73

def create!(attributes = nil, type = nil, &block)
  if attributes.is_a?(::Array)
    attributes.map { |attrs| create!(attrs, type, &block) }
  else
    doc = build(attributes, type, &block)
    base.persisted? ? doc.save! : raise_unsaved(doc)
    doc
  end
end

#find_or_create_by(attributes = nil, type = nil) ⇒ Document #find_or_create_by(attributes = nil, type = nil) ⇒ Document

Find the first document given the conditions, or creates a new document with the conditions that were supplied.

Examples:

Find or create.

person.posts.find_or_create_by(:title => "Testing")

Overloads:

  • #find_or_create_by(attributes = nil, type = nil) ⇒ Document

    Parameters:

    • attributes (Hash) (defaults to: nil)

      The attributes to search or create with.

    • type (Class) (defaults to: nil)

      The optional type of document to create.

  • #find_or_create_by(attributes = nil, type = nil) ⇒ Document

    Parameters:

    • attributes (Hash) (defaults to: nil)

      The attributes to search or create with.

    • type (Class) (defaults to: nil)

      The optional type of document to create.

Returns:

  • (Document)

    An existing document or newly created one.



98
99
100
# File 'lib/mongoid/relations/many.rb', line 98

def find_or_create_by(attrs = {}, type = nil, &block)
  find_or(:create, attrs, type, &block)
end

#find_or_create_by!(attributes = nil, type = nil) ⇒ Document #find_or_create_by!(attributes = nil, type = nil) ⇒ Document

Find the first document given the conditions, or creates a new document with the conditions that were supplied. This will raise an error if validation fails.

Examples:

Find or create.

person.posts.find_or_create_by!(:title => "Testing")

Overloads:

  • #find_or_create_by!(attributes = nil, type = nil) ⇒ Document

    Parameters:

    • attributes (Hash) (defaults to: nil)

      The attributes to search or create with.

    • type (Class) (defaults to: nil)

      The optional type of document to create.

  • #find_or_create_by!(attributes = nil, type = nil) ⇒ Document

    Parameters:

    • attributes (Hash) (defaults to: nil)

      The attributes to search or create with.

    • type (Class) (defaults to: nil)

      The optional type of document to create.

Returns:

  • (Document)

    An existing document or newly created one.

Raises:



119
120
121
# File 'lib/mongoid/relations/many.rb', line 119

def find_or_create_by!(attrs = {}, type = nil, &block)
  find_or(:create!, attrs, type, &block)
end

#find_or_initialize_by(attributes = {}, type = nil) ⇒ Document #find_or_initialize_by(attributes = {}, type = nil) ⇒ Document

Find the first Document given the conditions, or instantiates a new document with the conditions that were supplied

Examples:

Find or initialize.

person.posts.find_or_initialize_by(:title => "Test")

Overloads:

  • #find_or_initialize_by(attributes = {}, type = nil) ⇒ Document

    Parameters:

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

      The attributes to search or initialize with.

    • type (Class) (defaults to: nil)

      The optional subclass to build.

  • #find_or_initialize_by(attributes = {}, type = nil) ⇒ Document

    Parameters:

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

      The attributes to search or initialize with.

    • type (Class) (defaults to: nil)

      The optional subclass to build.

Returns:

  • (Document)

    An existing document or newly instantiated one.



138
139
140
# File 'lib/mongoid/relations/many.rb', line 138

def find_or_initialize_by(attrs = {}, type = nil, &block)
  find_or(:build, attrs, type, &block)
end

#nil?false

This proxy can never be nil.

Examples:

Is the proxy nil?

relation.nil?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0



150
151
152
# File 'lib/mongoid/relations/many.rb', line 150

def nil?
  false
end

#respond_to?(name, include_private = false) ⇒ true, false

Since method_missing is overridden we should override this as well.

Examples:

Does the proxy respond to the method?

relation.respond_to?(:name)

Parameters:

  • name (Symbol)

    The method name.

Returns:

  • (true, false)

    If the proxy responds to the method.

Since:

  • 2.0.0



164
165
166
167
# File 'lib/mongoid/relations/many.rb', line 164

def respond_to?(name, include_private = false)
  [].respond_to?(name, include_private) ||
    klass.respond_to?(name, include_private) || super
end

#scopedCriteria

This is public access to the relation’s criteria.

Examples:

Get the scoped relation.

relation.scoped

Returns:

Since:

  • 2.1.0



177
178
179
# File 'lib/mongoid/relations/many.rb', line 177

def scoped
  criteria
end

#serializable_hash(options = {}) ⇒ Hash

Gets the document as a serializable hash, used by ActiveModel’s JSON and XML serializers. This override is just to be able to pass the :include and :except options to get associations in the hash.

Examples:

Get the serializable hash.

relation.serializable_hash

Parameters:

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

    The options to pass.

Options Hash (options):

  • :include (Symbol)

    What relations to include

  • :only (Symbol)

    Limit the fields to only these.

  • :except (Symbol)

    Dont include these fields.

Returns:

  • (Hash)

    The documents, ready to be serialized.

Since:

  • 2.0.0.rc.6



197
198
199
# File 'lib/mongoid/relations/many.rb', line 197

def serializable_hash(options = {})
  target.map { |document| document.serializable_hash(options) }
end

#unscopedCriteria

Get a criteria for the embedded documents without the default scoping applied.

Examples:

Get the unscoped criteria.

person.addresses.unscoped

Returns:

Since:

  • 2.4.0



210
211
212
# File 'lib/mongoid/relations/many.rb', line 210

def unscoped
  criteria.unscoped
end