Class: Mongoid::Association::Referenced::HasMany
- Inherits:
-
Object
- Object
- Mongoid::Association::Referenced::HasMany
- Includes:
- Buildable, Mongoid::Association::Relatable
- Defined in:
- lib/mongoid/association/referenced/has_many.rb,
lib/mongoid/association/referenced/has_many/eager.rb,
lib/mongoid/association/referenced/has_many/proxy.rb,
lib/mongoid/association/referenced/has_many/binding.rb,
lib/mongoid/association/referenced/has_many/buildable.rb,
lib/mongoid/association/referenced/has_many/enumerable.rb
Overview
The has_many association.
Defined Under Namespace
Modules: Buildable Classes: Binding, Eager, Enumerable, Proxy
Constant Summary collapse
- 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 ].freeze
- VALID_OPTIONS =
The complete list of valid options for this association, including the shared ones.
(ASSOCIATION_OPTIONS + SHARED_OPTIONS).freeze
- FOREIGN_KEY_SUFFIX =
The default foreign key suffix.
'_id'.freeze
Constants included from Mongoid::Association::Relatable
Mongoid::Association::Relatable::PRIMARY_KEY_DEFAULT, Mongoid::Association::Relatable::SHARED_OPTIONS
Instance Attribute Summary
Attributes included from Mongoid::Association::Relatable
Instance Method Summary collapse
-
#add_polymorphic_criterion(criteria, object_class) ⇒ Mongoid::Criteria
Add polymorphic query criteria to a Criteria object, if this association is polymorphic.
-
#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.
-
#embedded? ⇒ false
Is this association type embedded?.
-
#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.
-
#polymorphic? ⇒ true, false
Is this association polymorphic?.
-
#relation ⇒ Association::HasMany::Proxy
Get the association proxy class for this association type.
-
#relation_complements ⇒ Array<Association>
The list of association complements.
-
#setup! ⇒ self
Setup the instance methods, fields, etc.
-
#setup_instance_methods! ⇒ self
Setup the instance methods on the class having this association type.
-
#stores_foreign_key? ⇒ true
Does this association type store the foreign key?.
-
#type ⇒ String?
The type of this association if it’s polymorphic.
-
#validation_default ⇒ true
The default for validation the association object.
Methods included from Buildable
Methods included from Mongoid::Association::Relatable
#==, #counter_cache_column_name, #create_relation, #destructive?, #extension, #foreign_key_check, #foreign_key_setter, #get_callbacks, #initialize, #inverse, #inverse_association, #inverse_class, #inverse_class_name, #inverse_setter, #inverse_type, #inverse_type_setter, #inverses, #key, #relation_class, #relation_class_name, #setter, #type_setter, #validate?
Methods included from Options
#as, #autobuilding?, #autosave, #cascading_callbacks?, #counter_cached?, #cyclic?, #dependent, #forced_nil_inverse?, #indexed?, #inverse_of, #order, #primary_key, #store_as, #touch_field
Methods included from Constrainable
Instance Method Details
#add_polymorphic_criterion(criteria, object_class) ⇒ Mongoid::Criteria
Add polymorphic query criteria to a Criteria object, if this association is
polymorphic.
163 164 165 166 167 168 169 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 163 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.
186 187 188 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 186 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.
139 140 141 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 139 def criteria(base) query_criteria(base.send(primary_key), base) end |
#embedded? ⇒ false
Is this association type embedded?
109 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 109 def ; false; end |
#foreign_key ⇒ String
Get the foreign key field on the inverse for saving the association reference.
99 100 101 102 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 99 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.
198 199 200 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 198 def nested_builder(attributes, ) Nested::Many.new(self, attributes, ) end |
#path(document) ⇒ Root
Get the path calculator for the supplied document.
212 213 214 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 212 def path(document) Mongoid::Atomic::Paths::Root.new(document) end |
#polymorphic? ⇒ true, false
Is this association polymorphic?
176 177 178 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 176 def polymorphic? @polymorphic ||= !!as end |
#relation ⇒ Association::HasMany::Proxy
Get the association proxy class for this association type.
130 131 132 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 130 def relation Proxy end |
#relation_complements ⇒ Array<Association>
The list of association complements.
60 61 62 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 60 def relation_complements @relation_complements ||= [ Referenced::BelongsTo ].freeze end |
#setup! ⇒ self
Setup the instance methods, fields, etc. on the association owning class.
69 70 71 72 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 69 def setup! setup_instance_methods! self end |
#setup_instance_methods! ⇒ self
Setup the instance methods on the class having this association type.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 79 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 |
#stores_foreign_key? ⇒ true
Does this association type store the foreign key?
123 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 123 def stores_foreign_key?; false; end |
#type ⇒ String?
Only relevant for polymorphic associations.
The type of this association if it’s polymorphic.
150 151 152 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 150 def type @type ||= "#{as}_type" if polymorphic? end |
#validation_default ⇒ true
The default for validation the association object.
116 |
# File 'lib/mongoid/association/referenced/has_many.rb', line 116 def validation_default; true; end |