Class: Mongoid::Association::Referenced::BelongsTo

Inherits:
Object
  • Object
show all
Includes:
Buildable, Mongoid::Association::Relatable
Defined in:
build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb,
build/mongoid-master/lib/mongoid/association/referenced/belongs_to/eager.rb,
build/mongoid-master/lib/mongoid/association/referenced/belongs_to/proxy.rb,
build/mongoid-master/lib/mongoid/association/referenced/belongs_to/binding.rb,
build/mongoid-master/lib/mongoid/association/referenced/belongs_to/buildable.rb

Overview

The BelongsTo 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.

Returns:

  • (Array<Symbol>)

    The extra valid options.

[
    :autobuild,
    :autosave,
    :counter_cache,
    :dependent,
    :foreign_key,
    :index,
    :polymorphic,
    :primary_key,
    :touch,
    :optional,
    :required,
    :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_FIELD_TYPE =

The type of the field holding the foreign key.

Returns:

  • (Object)
Object
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

#==, #bindable?, #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_setter, #inverses, #key, #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, #type

Methods included from Constrainable

#convert_to_foreign_key

Instance Method Details

#embedded?false

Is this association type embedded?

Returns:

  • (false)

    Always false.



76
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb', line 76

def embedded?; false; end

#foreign_keyString

Get the foreign key field for saving the association reference.

Returns:

  • (String)

    The foreign key field for saving the association reference.



86
87
88
89
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb', line 86

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

#inverse_typeString

The name of the field used to store the type of polymorphic association.

Returns:

  • (String)

    The field used to store the type of polymorphic association.



108
109
110
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb', line 108

def inverse_type
  (@inverse_type ||= "#{name}_type") if polymorphic?
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:



118
119
120
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb', line 118

def nested_builder(attributes, options)
  Nested::One.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.



130
131
132
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb', line 130

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.



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

def polymorphic?
  @polymorphic ||= !!@options[:polymorphic]
end

#relationAssociation::BelongsTo::Proxy

Get the association proxy class for this association type.

Returns:

  • (Association::BelongsTo::Proxy)

    The proxy class.



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

def relation
  Proxy
end

#relation_complementsArray<Association>

The list of association complements.

Returns:

  • (Array<Association>)

    The association complements.



55
56
57
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb', line 55

def relation_complements
  @relation_complements ||= [ HasMany, HasOne ].freeze
end

#scopeProc | Symbol | nil

Get the scope to be applied when querying the association.

Returns:

  • (Proc | Symbol | nil)

    The association scope, if any.



137
138
139
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb', line 137

def scope
  @options[:scope]
end

#setup!self

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

Returns:

  • (self)


62
63
64
65
66
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb', line 62

def setup!
  setup_instance_methods!
  @owner_class.aliased_fields[name.to_s] = foreign_key
  self
end

#stores_foreign_key?true

Does this association type store the foreign key?

Returns:

  • (true)

    Always true.



71
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb', line 71

def stores_foreign_key?; true; end

#validation_defaultfalse

The default for validation the association object.

Returns:

  • (false)

    Always false.



81
# File 'build/mongoid-master/lib/mongoid/association/referenced/belongs_to.rb', line 81

def validation_default; false; end