Class: Mongoid::Association::Many
- Inherits:
-
Association::Proxy
- Object
- Association::Proxy
- 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 Method Summary collapse
-
#blank? ⇒ true | false
Is the association empty?.
-
#cache_version(timestamp_column = :updated_at) ⇒ String
For compatibility with Rails' caching.
-
#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
Documentgiven 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.
Instance Method Details
#blank? ⇒ true | false
Is the association empty?
28 29 30 |
# File 'lib/mongoid/association/many.rb', line 28 def blank? !any? end |
#cache_version(timestamp_column = :updated_at) ⇒ String
For compatibility with Rails' caching. Returns a string based on the given timestamp, and includes the number of records in the relation in the version.
195 196 197 198 |
# File 'lib/mongoid/association/many.rb', line 195 def cache_version( = :updated_at) @cache_version ||= {} @cache_version[] ||= compute_cache_version() 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.
42 43 44 45 46 47 48 49 50 |
# File 'lib/mongoid/association/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, 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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mongoid/association/many.rb', line 65 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) Array(doc).each do |doc| doc.try(:run_pending_callbacks) end _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.
90 91 92 |
# File 'lib/mongoid/association/many.rb', line 90 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.
106 107 108 |
# File 'lib/mongoid/association/many.rb', line 106 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
120 121 122 |
# File 'lib/mongoid/association/many.rb', line 120 def find_or_initialize_by(attrs = {}, type = nil, &block) find_or(:build, attrs, type, &block) end |
#nil? ⇒ false
This proxy can never be nil.
130 131 132 |
# File 'lib/mongoid/association/many.rb', line 130 def nil? false end |
#respond_to?(name, include_private = false) ⇒ true | false
Since method_missing is overridden we should override this as well.
143 144 145 146 |
# File 'lib/mongoid/association/many.rb', line 143 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.
154 155 156 |
# File 'lib/mongoid/association/many.rb', line 154 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.
172 173 174 |
# File 'lib/mongoid/association/many.rb', line 172 def serializable_hash( = {}) _target.map { |document| document.serializable_hash() } end |
#unscoped ⇒ Criteria
Get a criteria for the embedded documents without the default scoping applied.
183 184 185 |
# File 'lib/mongoid/association/many.rb', line 183 def unscoped criteria.unscoped end |