Class: Mongoid::Association::Many
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Forwardable,
Proxy ,
Forwardable
|
|
Instance Chain:
|
|
Inherits: |
Mongoid::Association::Proxy
|
Defined in: | lib/mongoid/association/many.rb |
Overview
This is the superclass for all many to one and many to many association proxies.
Constant Summary
Proxy
- Inherited
Class Method Summary
Proxy
- Inherited
.apply_ordering | Apply ordering to the criteria if it was defined on the association. |
.new | Sets the target and the association metadata properties. |
Instance Attribute Summary
-
#blank? ⇒ true | false
readonly
Is the association empty?
-
#nil? ⇒ false
readonly
This proxy can never be nil.
Proxy
- Inherited
#_association, | |
#_base | Model instance for the base of the association. |
#_target | Model instance for one to one associations, or array of model instances for one to many associations, for the target of the association. |
::Mongoid::Threaded::Lifecycle
- Included
#_assigning | Begin the assignment of attributes. |
#_assigning? | Is the current thread in assigning mode? |
#_binding | Execute a block in binding mode. |
#_binding? | Is the current thread in binding mode? |
#_building | Execute a block in building mode. |
#_building? | Is the current thread in building mode? |
#_creating? | Is the current thread in creating mode? |
#_loading | Execute a block in loading mode. |
#_loading? | Is the current thread in loading mode? |
Instance Method Summary
-
#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
::Mongoid::Document
given the conditions, or instantiates a new document with the conditions that were supplied. -
#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.
- #_session private
-
#analyze_loaded_target(timestamp_column)
private
Return a 2-tuple of the number of elements in the relation, and the largest timestamp value.
-
#analyze_unloaded_target(timestamp_column)
private
Returns a 2-tuple of the number of elements in the relation, and the largest timestamp value.
-
#compute_cache_version(timestamp_column) ⇒ String
private
Computes the cache version for the relation using the given timestamp colum; see #cache_version.
-
#find_or(method, attrs = {}, type = nil, &block) ⇒ Document
private
Find the first object given the supplied attributes or create/initialize it.
Proxy
- Inherited
#extend_proxies | Allow extension to be an array and extend each module. |
#extend_proxy, | |
#klass | Get the class from the association, or return nil if no association present. |
#reset_unloaded | Resets the criteria inside the association proxy. |
#substitutable | The default substitutable object for an association proxy is the clone of the target. |
Marshalable
- Included
#marshal_dump | Provides the data needed to Marshal.dump an association proxy. |
#marshal_load | Takes the provided data and sets it back on the proxy. |
Constructor Details
This class inherits a constructor from Mongoid::Association::Proxy
Instance Attribute Details
#blank? ⇒ true
| false
(readonly)
Is the association empty?
# File 'lib/mongoid/association/many.rb', line 22
def blank? !any? end
#nil? ⇒ false
(readonly)
This proxy can never be nil.
# File 'lib/mongoid/association/many.rb', line 124
def nil? false end
Instance Method Details
#_session (private)
[ GitHub ]# File 'lib/mongoid/association/many.rb', line 196
def _session _base.send(:_session) end
#analyze_loaded_target(timestamp_column) (private)
Return a 2-tuple of the number of elements in the relation, and the largest timestamp value.
# File 'lib/mongoid/association/many.rb', line 242
def analyze_loaded_target( ) newest = _target.select { |elem| elem.respond_to?( ) } .max { |a, b| a[ ] <=> b[ ] } [ _target.length, newest ? newest[ ] : nil ] end
#analyze_unloaded_target(timestamp_column) (private)
Returns a 2-tuple of the number of elements in the relation, and the largest timestamp value. This will query the database to perform a $sum and a $max.
# File 'lib/mongoid/association/many.rb', line 251
def analyze_unloaded_target( ) pipeline = criteria .group(_id: nil, count: { '$sum' => 1 }, latest: { '$max' => "$#{}" }) .pipeline result = klass.collection.aggregate(pipeline).to_a.first result ? [ result["count"], result["latest"] ] : [ 0 ] 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.
# File 'lib/mongoid/association/many.rb', line 189
def cache_version( = :updated_at) @cache_version ||= {} @cache_version[ ] ||= compute_cache_version( ) end
#compute_cache_version(timestamp_column) ⇒ String (private)
Computes the cache version for the relation using the given timestamp colum; see #cache_version.
# File 'lib/mongoid/association/many.rb', line 222
def compute_cache_version( ) = .to_s loaded = _target.respond_to?(:_loaded?) ? _target._loaded? : # has_many true # embeds_many size, = loaded ? analyze_loaded_target( ) : analyze_unloaded_target( ) if "#{size}-#{ .utc.to_formatted_s(klass. )}" else size.to_s 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.
# File 'lib/mongoid/association/many.rb', line 36
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.
# File 'lib/mongoid/association/many.rb', line 59
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(method, attrs = {}, type = nil, &block) ⇒ Document (private)
Find the first object given the supplied attributes or create/initialize it.
# File 'lib/mongoid/association/many.rb', line 210
def find_or(method, attrs = {}, type = nil, &block) attrs[klass.discriminator_key] = type.discriminator_value if type where(attrs).first || send(method, attrs, type, &block) end
#find_or_create_by(attrs = {}, type = nil, &block) ⇒ Document
#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.
#find_or_initialize_by(attrs = {}, type = nil, &block) ⇒ Document
Find the first ::Mongoid::Document
given the conditions, or instantiates a new document with the conditions that were supplied
# File 'lib/mongoid/association/many.rb', line 114
def find_or_initialize_by(attrs = {}, type = nil, &block) find_or(:build, attrs, type, &block) end
#respond_to?(name, include_private = false) ⇒ true
| false
Since method_missing is overridden we should override this as well.
# File 'lib/mongoid/association/many.rb', line 137
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.
# File 'lib/mongoid/association/many.rb', line 148
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.
# File 'lib/mongoid/association/many.rb', line 166
def serializable_hash( = {}) _target.map { |document| document.serializable_hash( ) } end
#unscoped ⇒ Criteria
Get a criteria for the embedded documents without the default scoping applied.
# File 'lib/mongoid/association/many.rb', line 177
def unscoped criteria.unscoped end