Class: Mongoid::Association::Referenced::HasManyThrough
- Inherits:
-
Object
- Object
- Mongoid::Association::Referenced::HasManyThrough
- Includes:
- Mongoid::Association::Relatable
- Defined in:
- lib/mongoid/association/referenced/has_many_through.rb,
lib/mongoid/association/referenced/has_many_through/eager.rb,
lib/mongoid/association/referenced/has_many_through/proxy.rb
Overview
Metadata class for has_many :through associations.
Defined Under Namespace
Constant Summary collapse
- ASSOCIATION_OPTIONS =
The options available for this type of association, in addition to the common ones.
i[order source through].freeze
- VALID_OPTIONS =
The complete list of valid options for this association, including the shared ones.
(ASSOCIATION_OPTIONS + SHARED_OPTIONS).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
#name, #options, #owner_class, #parent_inclusions
Instance Method Summary collapse
-
#criteria(base) ⇒ Mongoid::Criteria
Return a Criteria scoped to the target documents reachable from base via the through association.
-
#embedded? ⇒ false
Is this association embedded?.
-
#relation ⇒ Class
The proxy class for this association type.
-
#relation_complements ⇒ Array
The list of association complements.
-
#setup! ⇒ self
Setup instance methods on the owner class.
-
#source_association ⇒ Mongoid::Association::Relatable
The source association metadata on the intermediate class.
-
#stores_foreign_key? ⇒ false
Through associations never store a foreign key on the owner document.
-
#through_association ⇒ Mongoid::Association::Relatable
The intermediate association metadata on the owner class.
-
#validation_default ⇒ false
The default for validating the association object.
Methods included from Mongoid::Association::Relatable
#==, #bindable?, #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?, #many_to_many?, #one?, #path, #relation_class, #relation_class_name, #setter, #try_relation_class, #type_setter, #validate?
Methods included from Options
#as, #autobuilding?, #autosave, #cascading_callbacks?, #counter_cached?, #cyclic?, #dependent, #fallback, #fallback?, #forced_nil_inverse?, #indexed?, #inverse_of, #order, #polymorphic?, #primary_key, #store_as, #touch_field, #touchable?, #type
Methods included from Constrainable
Instance Method Details
#criteria(base) ⇒ Mongoid::Criteria
Return a Criteria scoped to the target documents reachable from base via the through association. Performs two queries: one against the intermediate collection, one against the source collection.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 117 def criteria(base) through_crit = through_association.criteria(base) crit = if source_association.stores_foreign_key? # FK is on the intermediate (e.g. appointment.patient_id -> belongs_to :patient) target_pk = source_association.primary_key # '_id' on Patient source_fk = source_association.foreign_key # 'patient_id' on Appointment source_association.klass.where( target_pk => { '$in' => key_values(through_crit, source_fk) } ) else # FK is on the source (e.g. reader.book_id -> has_many :readers on Book) source_pk = source_association.primary_key # '_id' on intermediate (Book) source_fk = source_association.foreign_key # 'book_id' on Reader source_association.klass.where( source_fk => { '$in' => key_values(through_crit, source_pk) } ) end order ? crit.order_by(order) : crit end |
#embedded? ⇒ false
Is this association embedded?
43 44 45 |
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 43 def false end |
#relation ⇒ Class
The proxy class for this association type.
50 51 52 |
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 50 def relation Proxy end |
#relation_complements ⇒ Array
The list of association complements.
28 29 30 |
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 28 def relation_complements [].freeze end |
#setup! ⇒ self
Setup instance methods on the owner class.
35 36 37 38 |
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 35 def setup! setup_instance_methods! self end |
#source_association ⇒ Mongoid::Association::Relatable
The source association metadata on the intermediate class. Resolved lazily to allow forward references.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 89 def source_association @source_association ||= begin source_name = ([:source] || name.to_s.singularize).to_s assoc = through_association.klass.relations[source_name] || raise( Errors::InvalidRelationOption.new( @owner_class, name, :source, source_name ) ) if assoc.is_a?(Referenced::HasAndBelongsToMany) raise( Errors::InvalidRelationOption.new( @owner_class, name, :source, 'has_and_belongs_to_many is not supported as a :through source' ) ) end assoc end end |
#stores_foreign_key? ⇒ false
Through associations never store a foreign key on the owner document.
57 58 59 |
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 57 def stores_foreign_key? false end |
#through_association ⇒ Mongoid::Association::Relatable
The intermediate association metadata on the owner class. Resolved lazily to allow forward references.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 65 def through_association @through_association ||= begin assoc = @owner_class.relations[[:through].to_s] || raise( Errors::InvalidRelationOption.new( @owner_class, name, :through, [:through] ) ) if assoc. raise( Errors::InvalidRelationOption.new( @owner_class, name, :through, 'through association must be a referenced association, not embedded' ) ) end assoc end end |
#validation_default ⇒ false
The default for validating the association object.
141 142 143 |
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 141 def validation_default false end |