123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Reflection::BelongsToReflection

Do not use. This class is for internal use only.

Constant Summary

AssociationReflection - Inherited

FROZEN_EMPTY_ARRAY

Class Method Summary

Instance Attribute Summary

AssociationReflection - Inherited

#belongs_to?

Returns true if self is a belongs_to reflection.

#collection?

Returns whether or not this association reflection is for a collection association.

#deprecated?, #extensions, #foreign_type, #has_inverse?,
#has_one?

Returns true if self is a has_one reflection.

#has_scope?, #nested?,
#parent_reflection
#polymorphic?, #type,
#validate?

Returns whether or not the association should be validated as part of the parent's validation.

MacroReflection - Inherited

#active_record, #autosave=,
#name

Returns the name of the macro.

#options

Returns the hash of options used for the macro.

#scope, #plural_name

AbstractReflection - Inherited

#counter_must_be_updated_by_has_many?,
#has_active_cached_counter?

Returns whether this association has a counter cache and its column values were backfilled (and so it is used internally by methods like size/+any?+/etc).

#has_cached_counter?

Returns whether this association has a counter cache.

#inverse_updates_counter_cache?
#inverse_updates_counter_in_memory?, #strict_loading?, #through_reflection?

Instance Method Summary

AssociationReflection - Inherited

#active_record_primary_key,
#active_record_primary_key_definition

Same as active_record_primary_key but returns an ::ActiveRecord::Key.

#add_as_polymorphic_through, #add_as_source, #add_as_through, #association_class, #association_foreign_key,
#association_foreign_key_definition

Same as association_foreign_key but returns an ::ActiveRecord::Key.

#association_primary_key,
#association_primary_key_definition
#association_route, #association_route_chain, #check_eager_loadable!, #check_validity!,
#clear_association_scope_cache

Pass the concrete target class for polymorphic associations.

#collect_join_chain

A chain of reflections from this one back to the owner.

#compute_class, #foreign_key,
#foreign_key_definition

Same as foreign_key but returns an ::ActiveRecord::Key.

#freeze, #join_table,
#macro

Returns the macro type.

#polymorphic_inverse_of, #polymorphic_name,
#prepare_default_association_route

Resolves the default route ahead of the reflection being made shareable, so that every Ractor can read it from the frozen slot.

#source_reflection, #through_reflection, #association_route_cache, #association_route_class, #association_route_constraints, #association_route_target_key,
#automatic_inverse_of

returns either nil or the inverse association name that it finds.

#build_association_route,
#can_find_inverse_of_automatically?

Checks to see if the reflection doesn't have any options that prevent us from being able to guess the inverse automatically.

#current_default_association_route?

Compares loaded contexts by identity: a context matching the slot's was loaded when the slot was filled, so this needn't load anything.

#default_association_route

Slots contain [origin context, destination class, destination context, route].

#default_destination_class, #derive_class_name, #derive_fk_query_constraints, #derive_foreign_key, #derive_join_table, #initialize_copy,
#inverse_name

Attempts to find the inverse association name automatically.

#resolve_association_route,
#scope_allows_automatic_inverse_of?

Scopes on the potential inverse reflection prevent automatic inverse_of, since the scope could exclude the owner record we would inverse from.

#valid_inverse_reflection?

Checks if the inverse reflection that is returned from the automatic_inverse_of method is a valid reflection.

MacroReflection - Inherited

#==

Returns true if self and other_aggregation have the same name attribute, active_record attribute, and other_aggregation has an options hash assigned to it.

#compute_class,
#klass

Returns the class for the macro.

#scope_for, #derive_class_name, #normalize_options, #_klass

AbstractReflection - Inherited

#alias_candidate, #association_route_for_join, #association_scope_cache,
#build_association

Returns a new, unsaved instance of the associated class.

#build_scope, #chain, #check_validity_of_inverse!,
#class_name

Returns the class name for the macro.

#constraints, #counter_cache_column, #inverse_of,
#inverse_which_updates_counter_cache

We need to avoid the following situation:

#join_scope, #join_scopes, #klass_join_scope,
#scopes

Returns a list of scopes that should be applied for this ::ActiveRecord::Reflection object when querying the database.

#strict_loading_violation_message, #table_name,
#actual_source_reflection

FIXME: this is a horrible name.

#ensure_option_not_given_as_class!,
#inferred_primary_key_definition

The Key form of a model's inferred primary key.

#primary_key, #primary_key_definition

Constructor Details

This class inherits a constructor from ActiveRecord::Reflection::AssociationReflection

Instance Attribute Details

#belongs_to? ⇒ Boolean (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 1086

def belongs_to?; true; end

Instance Method Details

#association_class

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 1088

def association_class
  if polymorphic?
    Associations::BelongsToPolymorphicAssociation
  else
    Associations::BelongsToAssociation
  end
end

#association_primary_key(klass = nil)

klass option is necessary to support loading polymorphic associations

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 1097

def association_primary_key(klass = nil)
  association_primary_key_definition(klass).name
end

#association_primary_key_definition(klass = nil)

Same as #association_primary_key but returns an ::ActiveRecord::Key. When the association uses the target model's primary key, the model's cached key is returned so the common case doesn't allocate.

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 1104

def association_primary_key_definition(klass = nil) # :nodoc:
  if options[:primary_key]
    return @association_primary_key_definition ||= ActiveRecord::Key.for(options[:primary_key])
  end

  if polymorphic? && options[:inverse_of] && klass
    inverse = klass.reflect_on_association(options[:inverse_of])
    if inverse && inverse.options[:primary_key] && !inverse.options[:query_constraints]
      return inverse.active_record_primary_key_definition
    end
  end

  klass ||= self.klass

  if klass.has_query_constraints? && options[:foreign_key] && !options[:query_constraints]
    return inferred_primary_key_definition(klass)
  end

  if klass.has_query_constraints? || options[:query_constraints]
    klass.composite_query_constraints_definition
  else
    inferred_primary_key_definition(klass)
  end
end

#association_route_class (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 1147

def association_route_class
  AssociationRoute
end

#association_route_target_key(target_class) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 1143

def association_route_target_key(target_class)
  association_primary_key_definition(target_class)
end

#can_find_inverse_of_automatically? ⇒ Boolean (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 1151

def can_find_inverse_of_automatically?(*)
  !polymorphic? && super
end

#macro

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 1084

def macro; :belongs_to; end

#resolve_association_route(origin_class, destination_class) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/reflection.rb', line 1130

def resolve_association_route(origin_class, destination_class)
  if polymorphic?
    unless destination_class
      return AssociationRoute.new(link: AssociationLink.for(reference_key: foreign_key_definition, fixed_values: { foreign_type => nil }, reference_class: origin_class))
    end
    fixed_values = { foreign_type => -destination_class.polymorphic_name }
  else
    fixed_values = {}
  end

  build_association_route(reference_class: origin_class, target_class: destination_class, fixed_values: fixed_values)
end