Class: Mongoid::Association::Referenced::HasManyThrough

Inherits:
Object
  • Object
show all
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

Classes: Eager, Proxy

Constant Summary collapse

ASSOCIATION_OPTIONS =

The options available for this type of association, in addition to the common ones.

Returns:

  • (Array<Symbol>)

    The extra valid options.

i[order source through].freeze
VALID_OPTIONS =

The complete list of valid options for this association, including the shared ones.

Returns:

  • (Array<Symbol>)

    The valid options.

(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

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

#convert_to_foreign_key

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.

Parameters:

  • base (Document)

    The owner document.

Returns:



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?

Returns:

  • (false)


43
44
45
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 43

def embedded?
  false
end

#relationClass

The proxy class for this association type.

Returns:

  • (Class)


50
51
52
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 50

def relation
  Proxy
end

#relation_complementsArray

The list of association complements.

Returns:

  • (Array)


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.

Returns:

  • (self)


35
36
37
38
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 35

def setup!
  setup_instance_methods!
  self
end

#source_associationMongoid::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 = (@options[: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.

Returns:

  • (false)


57
58
59
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 57

def stores_foreign_key?
  false
end

#through_associationMongoid::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[@options[:through].to_s] ||
            raise(
              Errors::InvalidRelationOption.new(
                @owner_class, name, :through, @options[:through]
              )
            )
    if assoc.embedded?
      raise(
        Errors::InvalidRelationOption.new(
          @owner_class, name, :through,
          'through association must be a referenced association, not embedded'
        )
      )
    end
    assoc
  end
end

#validation_defaultfalse

The default for validating the association object.

Returns:

  • (false)


141
142
143
# File 'lib/mongoid/association/referenced/has_many_through.rb', line 141

def validation_default
  false
end