Class: Mongoid::Association::Referenced::HasMany

Inherits:
Object
  • Object
show all
Includes:
Buildable, Mongoid::Association::Relatable
Defined in:
build/mongoid-master/lib/mongoid/association/referenced/has_many.rb,
build/mongoid-master/lib/mongoid/association/referenced/has_many/eager.rb,
build/mongoid-master/lib/mongoid/association/referenced/has_many/proxy.rb,
build/mongoid-master/lib/mongoid/association/referenced/has_many/binding.rb,
build/mongoid-master/lib/mongoid/association/referenced/has_many/buildable.rb,
build/mongoid-master/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.

Returns:

  • (Array<Symbol>)

    The extra valid options.

[
    :after_add,
    :after_remove,
    :as,
    :autosave,
    :before_add,
    :before_remove,
    :dependent,
    :foreign_key,
    :order,
    :primary_key,
    :scope,
].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
FOREIGN_KEY_SUFFIX =

The default foreign key suffix.

Returns:

  • (String)

    '_id'

'_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

#name, #options, #parent_inclusions

Instance Method Summary collapse

Methods included from Buildable

#build

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, #forced_nil_inverse?, #indexed?, #inverse_of, #order, #primary_key, #store_as, #touch_field

Methods included from Constrainable

#convert_to_foreign_key

Instance Method Details

#add_polymorphic_criterion(criteria, object_class) ⇒ Mongoid::Criteria

Add polymorphic query criteria to a Criteria object, if this association is

polymorphic.

Parameters:

  • criteria (Mongoid::Criteria)

    The criteria object to add to.

  • object_class (Class)

    The object class.

Returns:



133
134
135
136
137
138
139
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 133

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.

Parameters:

  • doc (Document)

    The document to be bound.

Returns:

  • (true | false)

    Whether the document can be bound.



154
155
156
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 154

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.

Returns:



113
114
115
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 113

def criteria(base)
  query_criteria(base.send(primary_key), base)
end

#embedded?false

Is this association type embedded?

Returns:

  • (false)

    Always false.



91
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 91

def embedded?; false; end

#foreign_keyString

Get the foreign key field on the inverse for saving the association reference.

Returns:

  • (String)

    The foreign key field on the inverse for saving the association reference.



83
84
85
86
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 83

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.

Parameters:

  • attributes (Hash)

    The attributes to use to build the association object.

  • options (Hash)

    The options for the association.

Returns:



164
165
166
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 164

def nested_builder(attributes, options)
  Nested::Many.new(self, attributes, options)
end

#path(document) ⇒ Root

Get the path calculator for the supplied document.

Examples:

Get the path calculator.

Proxy.path(document)

Parameters:

  • document (Document)

    The document to calculate on.

Returns:

  • (Root)

    The root atomic path calculator.



176
177
178
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 176

def path(document)
  Mongoid::Atomic::Paths::Root.new(document)
end

#polymorphic?true | false

Is this association polymorphic?

Returns:

  • (true | false)

    Whether this association is polymorphic.



144
145
146
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 144

def polymorphic?
  @polymorphic ||= !!as
end

#relationAssociation::HasMany::Proxy

Get the association proxy class for this association type.

Returns:

  • (Association::HasMany::Proxy)

    The proxy class.



106
107
108
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 106

def relation
  Proxy
end

#relation_complementsArray<Association>

The list of association complements.

Returns:

  • (Array<Association>)

    The association complements.



50
51
52
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 50

def relation_complements
  @relation_complements ||= [ Referenced::BelongsTo ].freeze
end

#scopeProc | Symbol | nil

Get the scope to be applied when querying the association.

Returns:

  • (Proc | Symbol | nil)

    The association scope, if any.



183
184
185
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 183

def scope
  @options[:scope]
end

#setup!self

Setup the instance methods, fields, etc. on the association owning class.

Returns:

  • (self)


57
58
59
60
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 57

def setup!
  setup_instance_methods!
  self
end

#setup_instance_methods!self

Setup the instance methods on the class having this association type.

Returns:

  • (self)


65
66
67
68
69
70
71
72
73
74
75
76
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 65

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?

Returns:

  • (true)

    Always true.



101
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 101

def stores_foreign_key?; false; end

#typeString | nil

Note:

Only relevant for polymorphic associations.

The type of this association if it's polymorphic.

Returns:

  • (String | nil)

    The type field.



122
123
124
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 122

def type
  @type ||= "#{as}_type" if polymorphic?
end

#validation_defaulttrue

The default for validation the association object.

Returns:

  • (true)

    Always true.



96
# File 'build/mongoid-master/lib/mongoid/association/referenced/has_many.rb', line 96

def validation_default; true; end