Class: Mongoid::Relations::Many
- Inherits:
-
Proxy
- Object
- Proxy
- 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)
-
- (true, false) blank?
Is the relation empty?.
-
- (Document) create(attributes = nil, options = {}, type = nil, &block)
The newly created document.
-
- (Document) create!(attributes = nil, options = {}, type = nil, &block)
Creates a new document on the references many relation.
-
- (true, false) exists?
Determine if any documents in this relation exist in the database.
-
- (Document) find_or_create_by(attrs = {}, options = {}, type = nil, &block)
Find the first document given the conditions, or creates a new document with the conditions that were supplied.
-
- (Document) find_or_initialize_by(attrs = {}, options = {}, type = nil, &block)
Find the first Document given the conditions, or instantiates a new document with the conditions that were supplied.
-
- (false) nil?
This proxy can never be nil.
-
- (true, false) respond_to?(name, include_private = false)
Since method_missing is overridden we should override this as well.
-
- (Criteria) scoped
This is public access to the relation's criteria.
-
- (Hash) serializable_hash(options = {})
Gets the document as a serializable hash, used by ActiveModel's JSON and XML serializers.
-
- (Criteria) unscoped
Get a criteria for the embedded documents without the default scoping applied.
Methods inherited from Proxy
eager_load_ids, #init, #substitutable, #with
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Proxy
Instance Method Details
- (true, false) blank?
Is the relation empty?
20 21 22 |
# File 'lib/mongoid/relations/many.rb', line 20 def blank? size == 0 end |
- (Document) create(attributes = nil, options = {}, type = nil) - (Document) create(attributes = nil, type = nil)
The newly created document.
42 43 44 45 46 |
# File 'lib/mongoid/relations/many.rb', line 42 def create(attributes = nil, = {}, type = nil, &block) doc = build(attributes, , type, &block) base.persisted? ? doc.save : raise_unsaved(doc) doc end |
- (Document) create!(attributes = nil, options = {}, type = nil) - (Document) create!(attributes = nil, type = nil)
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.
69 70 71 72 73 |
# File 'lib/mongoid/relations/many.rb', line 69 def create!(attributes = nil, = {}, type = nil, &block) doc = build(attributes, , type, &block) base.persisted? ? doc.save! : raise_unsaved(doc) doc end |
- (true, false) exists?
Determine if any documents in this relation exist in the database.
81 82 83 |
# File 'lib/mongoid/relations/many.rb', line 81 def exists? count > 0 end |
- (Document) find_or_create_by(attributes = nil, options = {}, type = nil) - (Document) find_or_create_by(attributes = nil, type = nil)
Find the first document given the conditions, or creates a new document with the conditions that were supplied.
101 102 103 |
# File 'lib/mongoid/relations/many.rb', line 101 def find_or_create_by(attrs = {}, = {}, type = nil, &block) find_or(:create, attrs, , type, &block) end |
- (Document) find_or_initialize_by(attributes = {}, options = {}, type = nil) - (Document) find_or_initialize_by(attributes = {}, type = nil)
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 = {}, = {}, type = nil, &block) find_or(:build, attrs, , type, &block) end |
- (false) nil?
This proxy can never be nil.
133 134 135 |
# File 'lib/mongoid/relations/many.rb', line 133 def nil? false end |
- (true, false) respond_to?(name, include_private = false)
Since method_missing is overridden we should override this as well.
147 148 149 150 |
# File 'lib/mongoid/relations/many.rb', line 147 def respond_to?(name, include_private = false) [].respond_to?(name, include_private) || klass.respond_to?(name, include_private) || super end |
- (Criteria) scoped
This is public access to the relation's criteria.
160 161 162 |
# File 'lib/mongoid/relations/many.rb', line 160 def scoped criteria end |
- (Hash) serializable_hash(options = {})
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.
180 181 182 |
# File 'lib/mongoid/relations/many.rb', line 180 def serializable_hash( = {}) target.map { |document| document.serializable_hash() } end |
- (Criteria) unscoped
Get a criteria for the embedded documents without the default scoping applied.
193 194 195 |
# File 'lib/mongoid/relations/many.rb', line 193 def unscoped criteria.unscoped end |