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
#base, #loaded, #metadata, #target
Instance Method Summary collapse
-
#<<(*args) ⇒ Object
(also: #concat, #push)
Appends a document or array of documents to the relation.
-
#build(attributes = {}, type = nil, &block) ⇒ Document
(also: #new)
Builds a new document in the relation and appends it to the target.
-
#create(attributes = nil, type = nil, &block) ⇒ Document
Creates a new document on the references many relation.
-
#create!(attributes = nil, type = nil) ⇒ Document
Creates a new document on the references many relation.
-
#exists? ⇒ true, false
Determine if any documents in this relation exist in the database.
-
#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.
-
#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. -
#nil? ⇒ false
This proxy can never be nil.
-
#respond_to?(name) ⇒ true, false
Since method_missing is overridden we should override this as well.
-
#serializable_hash(options = {}) ⇒ Hash
Gets the document as a serializable hash, used by ActiveModel’s JSON and XML serializers.
-
#size ⇒ Integer
(also: #length)
Always returns the number of documents that are in memory.
Methods inherited from Proxy
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Proxy
Instance Method Details
#<<(*args) ⇒ Object Also known as: concat, push
Appends a document or array of documents to the relation. Will set the parent and update the index in the process.
24 25 26 27 28 29 30 31 |
# File 'lib/mongoid/relations/many.rb', line 24 def <<(*args) = (args) args.flatten.each do |doc| return doc unless doc append(doc, ) doc.save if base.persisted? && ![:binding] end end |
#build(attributes = {}, type = nil, &block) ⇒ Document Also known as: new
Builds a new document in the relation and appends it to the target. Takes an optional type if you want to specify a subclass.
45 46 47 48 49 50 51 52 |
# File 'lib/mongoid/relations/many.rb', line 45 def build(attributes = {}, type = nil, &block) instantiated(type).tap do |doc| doc.write_attributes(attributes) doc.identify append(doc, (:binding => true)) block.call(doc) if block end end |
#create(attributes = nil, type = nil, &block) ⇒ Document
Creates a new document on the references many relation. This will save the document if the parent has been persisted.
65 66 67 68 69 70 |
# File 'lib/mongoid/relations/many.rb', line 65 def create(attributes = nil, type = nil, &block) build(attributes, type).tap do |doc| block.call(doc) if block doc.save if base.persisted? end end |
#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.
85 86 87 88 89 |
# File 'lib/mongoid/relations/many.rb', line 85 def create!(attributes = nil, type = nil) build(attributes, type).tap do |doc| doc.save! if base.persisted? end end |
#exists? ⇒ true, false
Determine if any documents in this relation exist in the database.
97 98 99 |
# File 'lib/mongoid/relations/many.rb', line 97 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.
110 111 112 |
# File 'lib/mongoid/relations/many.rb', line 110 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
123 124 125 |
# File 'lib/mongoid/relations/many.rb', line 123 def find_or_initialize_by(attrs = {}, &block) find_or(:build, attrs, &block) end |
#nil? ⇒ false
This proxy can never be nil.
135 136 137 |
# File 'lib/mongoid/relations/many.rb', line 135 def nil? false end |
#respond_to?(name) ⇒ true, false
Since method_missing is overridden we should override this as well.
149 150 151 |
# File 'lib/mongoid/relations/many.rb', line 149 def respond_to?(name) [].respond_to?(name) || methods.include?(name) 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.
169 170 171 |
# File 'lib/mongoid/relations/many.rb', line 169 def serializable_hash( = {}) target.map { |document| document.serializable_hash() } end |
#size ⇒ Integer Also known as: length
Always returns the number of documents that are in memory.
181 182 183 |
# File 'lib/mongoid/relations/many.rb', line 181 def size target.size end |