Class: Mongoid::Association::Referenced::HasMany
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Classes:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | lib/mongoid/association/referenced/has_many.rb, lib/mongoid/association/referenced/has_many/binding.rb, lib/mongoid/association/referenced/has_many/buildable.rb, lib/mongoid/association/referenced/has_many/eager.rb, lib/mongoid/association/referenced/has_many/enumerable.rb, lib/mongoid/association/referenced/has_many/proxy.rb |
Overview
The has_many association.
Constant Summary
-
ASSOCIATION_OPTIONS =
The options available for this type of association, in addition to the common ones.
[ :after_add, :after_remove, :as, :autosave, :before_add, :before_remove, :dependent, :foreign_key, :order, :primary_key, :scope, ].freeze
-
FOREIGN_KEY_SUFFIX =
The default foreign key suffix.
'_id'.freeze
-
VALID_OPTIONS =
The complete list of valid options for this association, including the shared ones.
(ASSOCIATION_OPTIONS + SHARED_OPTIONS).freeze
::Mongoid::Association::Relatable
- Included
Instance Attribute Summary
-
#embedded? ⇒ false
readonly
Is this association type embedded?
-
#polymorphic? ⇒ true | false
readonly
Is this association polymorphic?
-
#stores_foreign_key? ⇒ true
readonly
Does this association type store the foreign key?
::Mongoid::Association::Relatable
- Included
#destructive? | Whether the dependent method is destructive. |
#in_to? | Is this association an embedded_in or belongs_to association? |
#many? | Is this association an embeds_many or has_many association? |
#name | The name of the association. |
#one? | Is this association an embeds_one or has_one association? |
#options | The options on this association. |
#parent_inclusions | The associations above this one in the inclusion tree. |
#parent_inclusions= | The associations above this one in the inclusion tree. |
#validate? | Whether the associated object(s) should be validated. |
::Mongoid::Association::Options
- Included
#autobuilding? | Whether the association is autobuilding. |
#autosave |
|
#autosave? | Alias for Options#autosave. |
#cascading_callbacks? | Whether the association has callbacks cascaded down from the parent. |
#counter_cached? | Whether the association is counter-cached. |
#cyclic? | Is the association cyclic. |
#forced_nil_inverse? | Whether the association has forced nil inverse (So no foreign keys are saved). |
#indexed? | Whether to index the primary or foreign key field. |
#polymorphic? | Whether this association is polymorphic. |
#touchable? | Whether the association object should be automatically touched when its inverse object is updated. |
Instance Method Summary
-
#add_polymorphic_criterion(criteria, object_class) ⇒ Mongoid::Criteria
deprecated
Deprecated.
in 9.0.x
-
#bindable?(doc) ⇒ true | false
Whether trying to bind an object using this association should raise an error.
-
#criteria(base) ⇒ Mongoid::Criteria
The criteria used for querying this association.
-
#foreign_key ⇒ String
Get the foreign key field on the inverse for saving the association reference.
-
#nested_builder(attributes, options) ⇒ Association::Nested::Many
The nested builder object.
-
#path(document) ⇒ Root
Get the path calculator for the supplied document.
-
#relation ⇒ Association::HasMany::Proxy
Get the association proxy class for this association type.
-
#relation_complements ⇒ Array<Mongoid::Association::Relatable>
The list of association complements.
-
#scope ⇒ Proc | Symbol | nil
Get the scope to be applied when querying the association.
-
#setup! ⇒ self
Setup the instance methods, fields, etc.
-
#setup_instance_methods! ⇒ self
Setup the instance methods on the class having this association type.
-
#type ⇒ String | nil
The type of this association if it’s polymorphic.
-
#validation_default ⇒ true
The default for validation the association object.
- #default_foreign_key_field private
- #default_primary_key private
- #determine_inverses(other) private
- #polymorphic_inverses(other) private
- #query_criteria(object, base) private
- #with_inverse_field_criterion(criteria) private
- #with_ordering(criteria) private
WithPolymorphicCriteria
- Included
#with_polymorphic_criterion | If the receiver represents a polymorphic association, applies the polymorphic search criteria to the given #criteria object. |
Buildable
- Included
#build | This method either takes an _id or an object and queries for the inverse side using the id or sets the object. |
#query? |
::Mongoid::Association::Relatable
- Included
#== | Compare this association to another. |
#bindable? | Whether trying to bind an object using this association should raise an error. |
#class_name | |
#counter_cache_column_name | Get the counter cache column name. |
#create_relation | Create an association proxy object using the owner and target. |
#extension | Get the extension. |
#foreign_key_check | Get the name of the method to check if the foreign key has changed. |
#foreign_key_setter | The name of the foreign key setter method. |
#get_callbacks | Get the callbacks for a given type. |
#initialize | Initialize the |
#inverse | Get the inverse name. |
#inverse_association | Get the inverse’s association metadata. |
#inverse_class | The class of the object owning this association. |
#inverse_class_name | The class name of the object owning this association. |
#inverse_klass | |
#inverse_setter | The name of the inverse setter method. |
#inverse_type | Get the inverse type. |
#inverse_type_setter | Gets the setter for the field that sets the type of document on a polymorphic association. |
#inverses | Get the inverse names. |
#key | The foreign key field if this association stores a foreign key. |
#klass | |
#path | The atomic path for this association. |
#relation_class | The class of the association object(s). |
#relation_class_name |
|
#setter | The name of the setter on this object for assigning an associated object. |
#type_setter | Get the type setter. |
#create_extension!, #default_inverse, #define_autosaver!, #define_builder!, #define_counter_cache_callbacks!, #define_creator!, #define_dependency!, #define_existence_check!, #define_getter!, #define_ids_getter!, #define_ids_setter!, #define_setter!, #define_touchable!, | |
#inverse_association_classes | Gets the model classes with inverse associations of this model. |
#namespace_hierarchy | Returns an array of classes/modules forming the namespace hierarchy where symbols referenced in the provided class/module would be looked up by Ruby. |
#polymorph!, | |
#resolve_name | Resolves the given class/module name in the context of the specified module, as Ruby would when a constant is referenced in the source. |
#setup_index!, #validate! |
::Mongoid::Association::Options
- Included
#as | Returns the name of the parent to a polymorphic child. |
#dependent | Specify what happens to the associated object when the owner is destroyed. |
#inverse_of | The name the owning object uses to refer to this association. |
#order | The custom sorting options on the association. |
#primary_key |
|
#store_as | The store_as option. |
#touch_field | The field for saving the associated object’s type. |
#type | The field for saving the associated object’s type. |
::Mongoid::Association::Constrainable
- Included
#convert_to_foreign_key | Convert the supplied object to the appropriate type to set as the foreign key for an association. |
#convert_polymorphic |
Instance Attribute Details
#embedded? ⇒ false
(readonly)
Is this association type embedded?
# File 'lib/mongoid/association/referenced/has_many.rb', line 94
def ; false; end
#polymorphic? ⇒ true
| false
(readonly)
Is this association polymorphic?
# File 'lib/mongoid/association/referenced/has_many.rb', line 154
def polymorphic? @polymorphic ||= !!as end
#stores_foreign_key? ⇒ true
(readonly)
Does this association type store the foreign key?
# File 'lib/mongoid/association/referenced/has_many.rb', line 104
def stores_foreign_key?; false; end
Instance Method Details
#add_polymorphic_criterion(criteria, object_class) ⇒ Mongoid::Criteria
in 9.0.x
Add polymorphic query criteria to a ::Mongoid::Criteria
object, if this association is
polymorphic.
It appears as if this method is an artifact left over from a refactoring that renamed it with_polymorphic_criterion
, and made it private. Regardless, this method isn’t referenced anywhere else, and is unlikely to be useful to external clients. We should remove it.
# File 'lib/mongoid/association/referenced/has_many.rb', line 142
def add_polymorphic_criterion(criteria, object_class) if polymorphic? criteria.where(type => object_class.name) else criteria end end
#bindable?(doc) ⇒ true
| false
Whether trying to bind an object using this association should raise an error.
# File 'lib/mongoid/association/referenced/has_many.rb', line 164
def bindable?(doc) forced_nil_inverse? || (!!inverse && doc.fields.keys.include?(foreign_key)) end
#criteria(base) ⇒ Mongoid::Criteria
The criteria used for querying this association.
# File 'lib/mongoid/association/referenced/has_many.rb', line 116
def criteria(base) query_criteria(base.send(primary_key), base) end
#default_foreign_key_field (private)
[ GitHub ]# File 'lib/mongoid/association/referenced/has_many.rb', line 199
def default_foreign_key_field @default_foreign_key_field ||= "#{inverse}#{FOREIGN_KEY_SUFFIX}" end
#default_primary_key (private)
[ GitHub ]# File 'lib/mongoid/association/referenced/has_many.rb', line 220
def default_primary_key PRIMARY_KEY_DEFAULT end
#determine_inverses(other) (private)
[ GitHub ]# File 'lib/mongoid/association/referenced/has_many.rb', line 207
def determine_inverses(other) matches = (other || relation_class).relations.values.select do |rel| relation_complements.include?(rel.class) && rel.relation_class_name == inverse_class_name end if matches.size > 1 return [ default_inverse.name ] if default_inverse raise Errors::AmbiguousRelationship.new(relation_class, @owner_class, name, matches) end matches.collect { |m| m.name } unless matches.blank? end
#foreign_key ⇒ String
Get the foreign key field on the inverse for saving the association reference.
# File 'lib/mongoid/association/referenced/has_many.rb', line 86
def foreign_key @foreign_key ||= @options[:foreign_key] ? @options[:foreign_key].to_s : default_foreign_key_field end
#nested_builder(attributes, options) ⇒ Association::Nested::Many
The nested builder object.
#path(document) ⇒ Root
Get the path calculator for the supplied document.
#polymorphic_inverses(other) (private)
[ GitHub ]# File 'lib/mongoid/association/referenced/has_many.rb', line 203
def polymorphic_inverses(other) [ as ] end
#query_criteria(object, base) (private)
[ GitHub ]# File 'lib/mongoid/association/referenced/has_many.rb', line 224
def query_criteria(object, base) crit = klass.criteria crit = crit.apply_scope(scope) crit = crit.where(foreign_key => object) crit = with_polymorphic_criterion(crit, base) crit.association = self crit.parent_document = base with_ordering(crit) end
#relation ⇒ Association::HasMany::Proxy
Get the association proxy class for this association type.
# File 'lib/mongoid/association/referenced/has_many.rb', line 109
def relation Proxy end
#relation_complements ⇒ Array<Mongoid::Association::Relatable>
The list of association complements.
# File 'lib/mongoid/association/referenced/has_many.rb', line 53
def relation_complements @relation_complements ||= [ Referenced::BelongsTo ].freeze end
#scope ⇒ Proc
| Symbol | nil
Get the scope to be applied when querying the association.
# File 'lib/mongoid/association/referenced/has_many.rb', line 193
def scope @options[:scope] end
#setup! ⇒ self
Setup the instance methods, fields, etc. on the association owning class.
# File 'lib/mongoid/association/referenced/has_many.rb', line 60
def setup! setup_instance_methods! self end
#setup_instance_methods! ⇒ self
Setup the instance methods on the class having this association type.
# File 'lib/mongoid/association/referenced/has_many.rb', line 68
def setup_instance_methods! define_getter! define_ids_getter! define_setter! define_ids_setter! define_existence_check! define_autosaver! polymorph! define_dependency! @owner_class.validates_associated(name) if validate? self end
#type ⇒ String | nil
Only relevant for polymorphic associations.
The type of this association if it’s polymorphic.
# File 'lib/mongoid/association/referenced/has_many.rb', line 125
def type @type ||= "#{as}_type" if polymorphic? end
#validation_default ⇒ true
The default for validation the association object.
# File 'lib/mongoid/association/referenced/has_many.rb', line 99
def validation_default; true; end