Class: Mongoid::Relations::Many

Inherits:
Proxy show all
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

#base, #loaded, #metadata, #target

Instance Method Summary collapse

Methods inherited from Proxy

#init, #substitutable

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



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

def blank?
  size == 0
end

#exists?true, false

Determine if any documents in this relation exist in the database.

Examples:

Are there persisted documents?

person.posts.exists?

Returns:

  • (true, false)

    True is persisted documents exist, false if not.



30
31
32
# File 'lib/mongoid/relations/many.rb', line 30

def exists?
  count > 0
end

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

Parameters:

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

    The attributes to search or create with.

Returns:

  • (Document)

    An existing document or newly created one.



43
44
45
# File 'lib/mongoid/relations/many.rb', line 43

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

#find_or_initialize_by(attrs = {}, &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.

Returns:

  • (Document)

    An existing document or newly instantiated one.



56
57
58
# File 'lib/mongoid/relations/many.rb', line 56

def find_or_initialize_by(attrs = {}, &block)
  find_or(:build, attrs, &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



68
69
70
# File 'lib/mongoid/relations/many.rb', line 68

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



82
83
84
85
# File 'lib/mongoid/relations/many.rb', line 82

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



95
96
97
# File 'lib/mongoid/relations/many.rb', line 95

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



115
116
117
# File 'lib/mongoid/relations/many.rb', line 115

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