Class: Mongoid::Association::Many

Inherits:
Proxy
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/mongoid/association/many.rb

Overview

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

Constant Summary

Constants inherited from Proxy

Proxy::KEEPER_METHODS

Instance Attribute Summary

Attributes inherited from Proxy

#_association, #_base, #_target

Instance Method Summary collapse

Methods inherited from Proxy

apply_ordering, #extend_proxies, #initialize, #klass, #reset_unloaded, #substitutable

Methods included from Marshalable

#marshal_dump, #marshal_load

Constructor Details

This class inherits a constructor from Mongoid::Association::Proxy

Instance Method Details

#blank?true | false

Is the association empty?

Examples:

Is the association empty??

person.addresses.blank?

Returns:

  • (true | false)

    If the association is empty or not.



22
23
24
# File 'lib/mongoid/association/many.rb', line 22

def blank?
  !any?
end

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

Creates a new document on the references many association. 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.



36
37
38
39
40
41
42
43
44
# File 'lib/mongoid/association/many.rb', line 36

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, type = nil, &block) ⇒ Document

Creates a new document on the references many association. 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:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongoid/association/many.rb', line 59

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)

    Array(doc).each do |doc|
      doc.try(:run_pending_callbacks)
    end

    _base.persisted? ? doc.save! : raise_unsaved(doc)
    doc
  end
end

#find_or_create_by(attrs = {}, type = nil, &block) ⇒ Document

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

@param [ Hash ] attrs The attributes to search or create with.
@param [ Class ] type The optional type of document to create.

Examples:

Find or create.

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

Returns:

  • (Document)

    An existing document or newly created one.



84
85
86
# File 'lib/mongoid/association/many.rb', line 84

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

#find_or_create_by!(attrs = {}, type = nil, &block) ⇒ 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")

Parameters:

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

    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:



100
101
102
# File 'lib/mongoid/association/many.rb', line 100

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

#find_or_initialize_by(attrs = {}, type = nil, &block) ⇒ 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")

Parameters:

  • attrs (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.



114
115
116
# File 'lib/mongoid/association/many.rb', line 114

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.



124
125
126
# File 'lib/mongoid/association/many.rb', line 124

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.

  • include_private (true | false) (defaults to: false)

    Whether to include private methods.

Returns:

  • (true | false)

    If the proxy responds to the method.



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

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 association’s criteria.

Examples:

Get the scoped association.

relation.scoped

Returns:



148
149
150
# File 'lib/mongoid/association/many.rb', line 148

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):

  • :except (Symbol | String | Array<Symbol | String>)

    Do not include these field(s).

  • :include (Symbol | String | Array<Symbol | String>)

    Which association(s) to include.

  • :only (Symbol | String | Array<Symbol | String>)

    Limit the field(s) to only these.

Returns:

  • (Hash)

    The documents, ready to be serialized.



166
167
168
# File 'lib/mongoid/association/many.rb', line 166

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:



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

def unscoped
  criteria.unscoped
end