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.
22 23 24 25 26 27 28 29 |
# File 'lib/mongoid/relations/many.rb', line 22 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.
43 44 45 46 47 48 49 50 |
# File 'lib/mongoid/relations/many.rb', line 43 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.
63 64 65 66 67 68 |
# File 'lib/mongoid/relations/many.rb', line 63 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.
83 84 85 86 87 |
# File 'lib/mongoid/relations/many.rb', line 83 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.
95 96 97 |
# File 'lib/mongoid/relations/many.rb', line 95 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.
108 109 110 |
# File 'lib/mongoid/relations/many.rb', line 108 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
121 122 123 |
# File 'lib/mongoid/relations/many.rb', line 121 def find_or_initialize_by(attrs = {}, &block) find_or(:build, attrs, &block) end |
#nil? ⇒ false
This proxy can never be nil.
133 134 135 |
# File 'lib/mongoid/relations/many.rb', line 133 def nil? false end |
#respond_to?(name) ⇒ true, false
Since method_missing is overridden we should override this as well.
147 148 149 |
# File 'lib/mongoid/relations/many.rb', line 147 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.
167 168 169 |
# File 'lib/mongoid/relations/many.rb', line 167 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.
179 180 181 |
# File 'lib/mongoid/relations/many.rb', line 179 def size target.size end |