Class: Mongoid::Association::Referenced::HasAndBelongsToMany
- Inherits:
-
Object
- Object
- Mongoid::Association::Referenced::HasAndBelongsToMany
- Includes:
- Buildable, Mongoid::Association::Relatable
- Defined in:
- lib/mongoid/association/referenced/has_and_belongs_to_many.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many/eager.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb,
lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb
Overview
The HasAndBelongsToMany type association.
Defined Under Namespace
Modules: Buildable Classes: Binding, Eager, Proxy
Constant Summary collapse
- ASSOCIATION_OPTIONS =
The options available for this type of association, in addition to the common ones.
%i[ after_add after_remove autosave before_add before_remove counter_cache dependent foreign_key index order primary_key inverse_primary_key inverse_foreign_key scope ].freeze
- VALID_OPTIONS =
The complete list of valid options for this association, including the shared ones.
(ASSOCIATION_OPTIONS + SHARED_OPTIONS).freeze
- FOREIGN_KEY_FIELD_TYPE =
The type of the field holding the foreign key.
Array- FOREIGN_KEY_SUFFIX =
The default foreign key suffix.
'_ids'
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
#name, #options, #owner_class, #parent_inclusions
Instance Method Summary collapse
-
#bindable?(doc) ⇒ true | false
Whether trying to bind an object using this association should raise an error.
-
#criteria(base, id_list = nil) ⇒ Mongoid::Criteria
The criteria used for querying this association.
-
#embedded? ⇒ false
Is this association type embedded?.
-
#forced_nil_inverse? ⇒ true | false
Are ids only saved on this side of the association?.
-
#foreign_key ⇒ String
Get the foreign key field for saving the association reference.
-
#inverse_foreign_key ⇒ String
Get the foreign key field on the inverse.
-
#inverse_foreign_key_setter ⇒ String
Get the foreign key setter on the inverse.
-
#nested_builder(attributes, options) ⇒ Association::Nested::One
The nested builder object.
-
#path(document) ⇒ Root
Get the path calculator for the supplied document.
-
#relation ⇒ Association::HasAndBelongsToMany::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.
-
#stores_foreign_key? ⇒ true
Does this association type store the foreign key?.
-
#validation_default ⇒ false
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, #in_to?, #initialize, #inverse, #inverse_association, #inverse_class, #inverse_class_name, #inverse_setter, #inverse_type, #inverse_type_setter, #inverses, #key, #many?, #one?, #relation_class, #relation_class_name, #setter, #type_setter, #validate?
Methods included from Options
#as, #autobuilding?, #autosave, #cascading_callbacks?, #counter_cached?, #cyclic?, #dependent, #indexed?, #inverse_of, #order, #polymorphic?, #primary_key, #store_as, #touch_field, #touchable?, #type
Methods included from Constrainable
Instance Method Details
#bindable?(doc) ⇒ true | false
Whether trying to bind an object using this association should raise an error.
143 144 145 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 143 def bindable?(doc) forced_nil_inverse? || (!!inverse && doc.fields.keys.include?(foreign_key)) end |
#criteria(base, id_list = nil) ⇒ Mongoid::Criteria
The criteria used for querying this association.
117 118 119 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 117 def criteria(base, id_list = nil) query_criteria(id_list || base.send(foreign_key)) end |
#embedded? ⇒ false
Is this association type embedded?
71 72 73 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 71 def false end |
#forced_nil_inverse? ⇒ true | false
Are ids only saved on this side of the association?
85 86 87 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 85 def forced_nil_inverse? @forced_nil_inverse ||= @options.key?(:inverse_of) && !@options[:inverse_of] end |
#foreign_key ⇒ String
Get the foreign key field for saving the association reference.
106 107 108 109 110 111 112 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 106 def foreign_key @foreign_key ||= if @options[:foreign_key] @options[:foreign_key].to_s else default_foreign_key_field end end |
#inverse_foreign_key ⇒ String
Get the foreign key field on the inverse.
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 125 def inverse_foreign_key if @options.key?(:inverse_foreign_key) @options[:inverse_foreign_key] elsif @options.key?(:inverse_of) inverse_of ? "#{inverse_of.to_s.singularize}#{FOREIGN_KEY_SUFFIX}" : nil elsif inv = inverse_association&.foreign_key inv else "#{inverse_class_name.demodulize.underscore}#{FOREIGN_KEY_SUFFIX}" end end |
#inverse_foreign_key_setter ⇒ String
Get the foreign key setter on the inverse.
151 152 153 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 151 def inverse_foreign_key_setter @inverse_foreign_key_setter ||= "#{inverse_foreign_key}=" if inverse_foreign_key end |
#nested_builder(attributes, options) ⇒ Association::Nested::One
The nested builder object.
161 162 163 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 161 def nested_builder(attributes, ) Nested::Many.new(self, attributes, ) end |
#path(document) ⇒ Root
Get the path calculator for the supplied document.
173 174 175 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 173 def path(document) Mongoid::Atomic::Paths::Root.new(document) end |
#relation ⇒ Association::HasAndBelongsToMany::Proxy
Get the association proxy class for this association type.
99 100 101 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 99 def relation Proxy end |
#relation_complements ⇒ Array<Mongoid::Association::Relatable>
The list of association complements.
56 57 58 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 56 def relation_complements @relation_complements ||= [ self.class ].freeze end |
#scope ⇒ Proc | Symbol | nil
Get the scope to be applied when querying the association.
180 181 182 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 180 def scope @options[:scope] end |
#setup! ⇒ self
Setup the instance methods, fields, etc. on the association owning class.
63 64 65 66 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 63 def setup! setup_instance_methods! self end |
#stores_foreign_key? ⇒ true
Does this association type store the foreign key?
92 93 94 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 92 def stores_foreign_key? true end |
#validation_default ⇒ false
The default for validation the association object.
78 79 80 |
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 78 def validation_default true end |