Class: Mongoid::Association::Referenced::HasAndBelongsToMany

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

Since:

  • 7.0

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.

Returns:

  • (Array<Symbol>)

    The extra valid options.

Since:

  • 7.0

[
    :after_add,
    :after_remove,
    :autosave,
    :before_add,
    :before_remove,
    :counter_cache,
    :dependent,
    :foreign_key,
    :index,
    :order,
    :primary_key
].freeze
VALID_OPTIONS =

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

Returns:

  • (Array<Symbol>)

    The valid options.

Since:

  • 7.0

(ASSOCIATION_OPTIONS + SHARED_OPTIONS).freeze
FOREIGN_KEY_FIELD_TYPE =

The type of the field holding the foreign key.

Returns:

  • (Array)

Since:

  • 7.0

Array
FOREIGN_KEY_SUFFIX =

The default foreign key suffix.

Returns:

  • (String)

    '_ids'

Since:

  • 7.0

'_ids'.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

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, #initialize, #inverse, #inverse_association, #inverse_class, #inverse_class_name, #inverse_setter, #inverse_type, #inverse_type_setter, #inverses, #key, #klass, #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, #type

Methods included from Constrainable

#convert_to_foreign_key

Instance Method Details

#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.

Since:

  • 7.0



156
157
158
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 156

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 relation.

Returns:

Since:

  • 7.0



132
133
134
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 132

def criteria(base, id_list = nil)
  query_criteria(id_list || base.send(foreign_key))
end

#embedded?false

Is this association type embedded?

Returns:

  • (false)

    Always false.

Since:

  • 7.0



83
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 83

def embedded?; false; end

#forced_nil_inverse?true, false

Are ids only saved on this side of the relation?

Returns:

  • (true, false)

    Whether this association has a forced nil inverse.

Since:

  • 7.0



97
98
99
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 97

def forced_nil_inverse?
  @forced_nil_inverse ||= @options.key?(:inverse_of) && !@options[:inverse_of]
end

#foreign_keyString

Get the foreign key field for saving the association reference.

Returns:

  • (String)

    The foreign key field for saving the association reference.

Since:

  • 7.0



122
123
124
125
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 122

def foreign_key
  @foreign_key ||= @options[:foreign_key] ? @options[:foreign_key].to_s :
                     default_foreign_key_field
end

#inverse_foreign_keyString

Get the foreign key field on the inverse.

Returns:

  • (String)

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

Since:

  • 7.0



142
143
144
145
146
147
148
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 142

def inverse_foreign_key
  if @options.key?(:inverse_of)
    inverse_of ? "#{inverse_of.to_s.singularize}#{FOREIGN_KEY_SUFFIX}" : nil
  else
    "#{inverse_class_name.demodulize.underscore}#{FOREIGN_KEY_SUFFIX}"
  end
end

#inverse_foreign_key_setterString

Get the foreign key setter on the inverse.

Returns:

  • (String)

    The foreign key setter for saving the association reference on the inverse side.

Since:

  • 7.0



166
167
168
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 166

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.

Parameters:

  • attributes (Hash)

    The attributes to use to build the association object.

  • options (Hash)

    The options for the association.

Returns:

Since:

  • 7.0



178
179
180
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 178

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.

association.path(document)

Parameters:

  • document (Document)

    The document to calculate on.

Returns:

  • (Root)

    The root atomic path calculator.

Since:

  • 2.1.0



192
193
194
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 192

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

#relationAssociation::HasAndBelongsToMany::Proxy

Get the relation proxy class for this association type.

Returns:

  • (Association::HasAndBelongsToMany::Proxy)

    The proxy class.

Since:

  • 7.0



113
114
115
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 113

def relation
  Proxy
end

#relation_complementsArray<Association>

The list of association complements.

Returns:

  • (Array<Association>)

    The association complements.

Since:

  • 7.0



64
65
66
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 64

def relation_complements
  @relation_complements ||= [ self.class ].freeze
end

#setup!self

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

Returns:

  • (self)

Since:

  • 7.0



73
74
75
76
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 73

def setup!
  setup_instance_methods!
  self
end

#stores_foreign_key?true

Does this association type store the foreign key?

Returns:

  • (true)

    Always true.

Since:

  • 7.0



106
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 106

def stores_foreign_key?; true; end

#validation_defaultfalse

The default for validation the association object.

Returns:

  • (false)

    Always false.

Since:

  • 7.0



90
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many.rb', line 90

def validation_default; true; end