class Mongoid::Association::Referenced::BelongsTo

The BelongsTo type association.

Constants

ASSOCIATION_OPTIONS

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

@return [ Array<Symbol> ] The extra valid options.

FOREIGN_KEY_FIELD_TYPE

The type of the field holding the foreign key.

@return [ Object ]

FOREIGN_KEY_SUFFIX

The default foreign key suffix.

@return [ String ] ‘_id’

VALID_OPTIONS

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

@return [ Array<Symbol> ] The valid options.

Public Instance Methods

embedded?() click to toggle source

Is this association type embedded?

@return [ false ] Always false.

# File lib/mongoid/association/referenced/belongs_to.rb, line 77
def embedded?; false; end
foreign_key() click to toggle source

Get the foreign key field for saving the association reference.

@return [ String ] The foreign key field for saving the association reference.

# File lib/mongoid/association/referenced/belongs_to.rb, line 87
def foreign_key
  @foreign_key ||= @options[:foreign_key] ? @options[:foreign_key].to_s :
                     default_foreign_key_field
end
inverse_type() click to toggle source

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

@return [ String ] The field used to store the type of polymorphic association.

# File lib/mongoid/association/referenced/belongs_to.rb, line 124
def inverse_type
  (@inverse_type ||= "#{name}_type") if polymorphic?
end
nested_builder(attributes, options) click to toggle source

The nested builder object.

@param [ Hash ] attributes The attributes to use to build the association object. @param [ Hash ] options The options for the association.

@return [ Association::Nested::One ] The Nested Builder object.

# File lib/mongoid/association/referenced/belongs_to.rb, line 134
def nested_builder(attributes, options)
  Nested::One.new(self, attributes, options)
end
path(document) click to toggle source

Get the path calculator for the supplied document.

@example Get the path calculator.

association.path(document)

@param [ Document ] document The document to calculate on.

@return [ Root ] The root atomic path calculator.

# File lib/mongoid/association/referenced/belongs_to.rb, line 146
def path(document)
  Mongoid::Atomic::Paths::Root.new(document)
end
polymorphic?() click to toggle source

Is this association polymorphic?

@return [ true | false ] Whether this association is polymorphic.

# File lib/mongoid/association/referenced/belongs_to.rb, line 102
def polymorphic?
  @polymorphic ||= !!@options[:polymorphic]
end
relation() click to toggle source

Get the association proxy class for this association type.

@return [ Association::BelongsTo::Proxy ] The proxy class.

# File lib/mongoid/association/referenced/belongs_to.rb, line 95
def relation
  Proxy
end
relation_complements() click to toggle source

The list of association complements.

@return [ Array<Mongoid::Association::Relatable> ] The association complements.

# File lib/mongoid/association/referenced/belongs_to.rb, line 56
def relation_complements
  @relation_complements ||= [ HasMany, HasOne ].freeze
end
resolver() click to toggle source

Returns the object responsible for converting polymorphic type references into class objects, and vice versa. This is obtained via the ‘:polymorphic` option that was given when the association was defined.

See Mongoid::ModelResolver.resolver for how the ‘:polymorphic` option is interpreted here.

@raise KeyError if no such resolver has been registered under the given

identifier.

@return [ nil | Mongoid::ModelResolver ] the resolver to use

# File lib/mongoid/association/referenced/belongs_to.rb, line 117
def resolver
  @resolver ||= Mongoid::ModelResolver.resolver(@options[:polymorphic])
end
scope() click to toggle source

Get the scope to be applied when querying the association.

@return [ Proc | Symbol | nil ] The association scope, if any.

# File lib/mongoid/association/referenced/belongs_to.rb, line 153
def scope
  @options[:scope]
end
setup!() click to toggle source

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

@return [ self ]

# File lib/mongoid/association/referenced/belongs_to.rb, line 63
def setup!
  setup_instance_methods!
  @owner_class.aliased_fields[name.to_s] = foreign_key
  self
end
stores_foreign_key?() click to toggle source

Does this association type store the foreign key?

@return [ true ] Always true.

# File lib/mongoid/association/referenced/belongs_to.rb, line 72
def stores_foreign_key?; true; end
validation_default() click to toggle source

The default for validation the association object.

@return [ false ] Always false.

# File lib/mongoid/association/referenced/belongs_to.rb, line 82
def validation_default; false; end

Private Instance Methods

create_foreign_key_field!() click to toggle source
# File lib/mongoid/association/referenced/belongs_to.rb, line 229
def create_foreign_key_field!
  @owner_class.field(
      foreign_key,
      type: FOREIGN_KEY_FIELD_TYPE,
      identity: true,
      overwrite: true,
      association: self,
      default: nil
  )
end
default_foreign_key_field() click to toggle source
# File lib/mongoid/association/referenced/belongs_to.rb, line 188
def default_foreign_key_field
  @default_foreign_key_field ||= "#{name}#{FOREIGN_KEY_SUFFIX}"
end
default_primary_key() click to toggle source
# File lib/mongoid/association/referenced/belongs_to.rb, line 184
def default_primary_key
  PRIMARY_KEY_DEFAULT
end
determine_inverses(other) click to toggle source
# File lib/mongoid/association/referenced/belongs_to.rb, line 210
def determine_inverses(other)
  matches = (other || relation_class).relations.values.select do |rel|
    relation_complements.include?(rel.class) &&
        rel.relation_class_name == inverse_class_name

  end
  if matches.size > 1
    raise Errors::AmbiguousRelationship.new(relation_class, @owner_class, name, matches)
  end
  matches.collect { |m| m.name }
end
index_spec() click to toggle source
# File lib/mongoid/association/referenced/belongs_to.rb, line 176
def index_spec
  if polymorphic?
    { key => 1, inverse_type => 1 }
  else
    { key => 1 }
  end
end
polymorph!() click to toggle source
# File lib/mongoid/association/referenced/belongs_to.rb, line 192
def polymorph!
  if polymorphic?
    @owner_class.polymorphic = true
    @owner_class.field(inverse_type, type: String)
  end
end
polymorphic_inverses(other = nil) click to toggle source
# File lib/mongoid/association/referenced/belongs_to.rb, line 199
def polymorphic_inverses(other = nil)
  if other
    matches = other.relations.values.select do |rel|
      relation_complements.include?(rel.class) &&
          rel.as == name &&
          rel.relation_class_name == inverse_class_name
    end
    matches.collect { |m| m.name }
  end
end
require_association?() click to toggle source

If set to true, then the associated object will be validated when this object is saved

# File lib/mongoid/association/referenced/belongs_to.rb, line 223
def require_association?
  required = @options[:required] if @options.key?(:required)
  required = !@options[:optional] if @options.key?(:optional) && required.nil?
  required.nil? ? Mongoid.belongs_to_required_by_default : required
end
setup_instance_methods!() click to toggle source
# File lib/mongoid/association/referenced/belongs_to.rb, line 159
def setup_instance_methods!
  define_getter!
  define_setter!
  define_existence_check!
  define_builder!
  define_creator!
  define_autosaver!
  define_counter_cache_callbacks!
  polymorph!
  define_dependency!
  create_foreign_key_field!
  setup_index!
  define_touchable!
  @owner_class.validates_associated(name) if validate? || require_association?
  @owner_class.validates(name, presence: true) if require_association?
end