Class: Mongoid::Relations::Many
- 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
Instance Attribute Summary
Attributes inherited from Proxy
Instance Method Summary collapse
-
#blank? ⇒ true, false
Is the relation empty?.
-
#create(attributes = nil, type = nil, &block) ⇒ Document
Creates a new document on the references many relation.
-
#create!(attributes = nil, type = nil, &block) ⇒ Document
Creates a new document on the references many relation.
-
#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 relation’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, #with
Methods included from Marshalable
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?
20 21 22 |
# File 'lib/mongoid/relations/many.rb', line 20 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.
42 43 44 45 46 47 48 49 50 |
# File 'lib/mongoid/relations/many.rb', line 42 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.
72 73 74 75 76 77 78 79 80 |
# File 'lib/mongoid/relations/many.rb', line 72 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.
97 98 99 |
# File 'lib/mongoid/relations/many.rb', line 97 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
116 117 118 |
# File 'lib/mongoid/relations/many.rb', line 116 def find_or_initialize_by(attrs = {}, type = nil, &block) find_or(:build, attrs, type, &block) end |
#nil? ⇒ false
This proxy can never be nil.
128 129 130 |
# File 'lib/mongoid/relations/many.rb', line 128 def nil? false end |
#respond_to?(name, include_private = false) ⇒ true, false
Since method_missing is overridden we should override this as well.
142 143 144 145 |
# File 'lib/mongoid/relations/many.rb', line 142 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 relation’s criteria.
155 156 157 |
# File 'lib/mongoid/relations/many.rb', line 155 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.
175 176 177 |
# File 'lib/mongoid/relations/many.rb', line 175 def serializable_hash( = {}) target.map { |document| document.serializable_hash() } end |
#unscoped ⇒ Criteria
Get a criteria for the embedded documents without the default scoping applied.
188 189 190 |
# File 'lib/mongoid/relations/many.rb', line 188 def unscoped criteria.unscoped end |