Class: Mongoid::Association::Many
- 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.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Proxy
#_association, #_base, #_target
Instance Method Summary collapse
-
#blank? ⇒ true, false
Is the association empty?.
-
#create(attributes = nil, type = nil, &block) ⇒ Document
Creates a new document on the references many association.
-
#create!(attributes = nil, type = nil, &block) ⇒ Document
Creates a new document on the references many association.
-
#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.
-
#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.
-
#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. -
#nil? ⇒ false
This proxy can never be nil.
-
#respond_to?(name, include_private = false) ⇒ true, false
Since method_missing is overridden we should override this as well.
-
#scoped ⇒ Criteria
This is public access to the association’s criteria.
-
#serializable_hash(options = {}) ⇒ Hash
Gets the document as a serializable hash, used by ActiveModel’s JSON and XML serializers.
-
#unscoped ⇒ Criteria
Get a criteria for the embedded documents without the default scoping applied.
Methods inherited from Proxy
apply_ordering, #extend_proxies, #init, #klass, #reset_unloaded, #substitutable
Methods included from Marshalable
Instance Method Details
#blank? ⇒ true, false
Is the association empty?
21 22 23 |
# File 'lib/mongoid/association/many.rb', line 21 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.
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.
59 60 61 62 63 64 65 66 67 |
# 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) _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.
79 80 81 |
# File 'lib/mongoid/association/many.rb', line 79 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.
95 96 97 |
# File 'lib/mongoid/association/many.rb', line 95 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
109 110 111 |
# File 'lib/mongoid/association/many.rb', line 109 def find_or_initialize_by(attrs = {}, type = nil, &block) find_or(:build, attrs, type, &block) end |
#nil? ⇒ false
This proxy can never be nil.
119 120 121 |
# File 'lib/mongoid/association/many.rb', line 119 def nil? false end |
#respond_to?(name, include_private = false) ⇒ true, false
Since method_missing is overridden we should override this as well.
132 133 134 135 |
# File 'lib/mongoid/association/many.rb', line 132 def respond_to?(name, include_private = false) [].respond_to?(name, include_private) || klass.respond_to?(name, include_private) || super end |
#scoped ⇒ Criteria
This is public access to the association’s criteria.
143 144 145 |
# File 'lib/mongoid/association/many.rb', line 143 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.
161 162 163 |
# File 'lib/mongoid/association/many.rb', line 161 def serializable_hash( = {}) _target.map { |document| document.serializable_hash() } end |
#unscoped ⇒ Criteria
Get a criteria for the embedded documents without the default scoping applied.
172 173 174 |
# File 'lib/mongoid/association/many.rb', line 172 def unscoped criteria.unscoped end |