123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Associations::BelongsToPolymorphicAssociation

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

Overview

Active Record Belongs To Polymorphic Association

Class Method Summary

Association - Inherited

Instance Attribute Summary

BelongsToAssociation - Inherited

Association - Inherited

#disable_joins,
#loaded?

Has the target been already loaded?

#options, #owner, #reflection,
#stale_target?

The target is stale if the target no longer points to the record(s) that the relevant foreign_key(s) refers to.

#target,
#target=

Sets the target of this association to \target, and the loaded flag to true.

#find_target, #find_target?,
#foreign_key_present?

Returns true if there is a foreign key present on the owner which references the target.

#violates_strict_loading?

Instance Method Summary

BelongsToAssociation - Inherited

SingularAssociation - Inherited

#build,
#force_reload_reader

Implements the reload reader method, e.g.

#reader

Implements the reader method, e.g.

#writer

Implements the writer method, e.g.

#_create_record, #find_target, #replace, #scope_for_create, #set_new_record

Association - Inherited

#create, #create!, #extensions, #initialize_attributes, #inversed_from, #inversed_from_queries,
#klass

Returns the class of the target.

#load_target

Loads the target if needed and returns it.

#loaded!

Asserts the target has been loaded setting the loaded flag to true.

#marshal_dump

We can’t dump @reflection and @through_reflection since it contains the scope proc.

#marshal_load,
#reload

Reloads the target and returns self on success.

#remove_inverse_instance

Remove the inverse association, if possible.

#reset

Resets the loaded flag to false and sets the target to nil.

#reset_negative_cache, #reset_scope, #scope,
#set_inverse_instance

Set the inverse association, if possible.

#set_inverse_instance_from_queries,
#association_scope

The scope for this association.

#build_record, #enqueue_destroy_association,
#ensure_klass_exists!

Reader and writer methods call this so that consistent errors are presented when the association target class does not exist.

#foreign_key_for?

Returns true if record contains the foreign_key.

#inversable?, #inverse_association_for,
#inverse_reflection_for

Can be redefined by subclasses, notably polymorphic belongs_to The record parameter is necessary to support polymorphic inverses as we must check for the association in the specific class of the record.

#invertible_for?

Returns true if inverse association on the given record needs to be set.

#matches_foreign_key?,
#raise_on_type_mismatch!

Raises ::ActiveRecord::AssociationTypeMismatch unless record is of the kind of the class of the associated objects.

#scope_for_create,
#skip_statement_cache?

Returns true if statement cache should be skipped on the association reader.

#skip_strict_loading,
#stale_state

This should be implemented to return the values of the relevant key(s) on the owner, so that when stale_state is different from the value stored on the last find_target, the target is stale.

#target_scope

Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e.

Constructor Details

This class inherits a constructor from ActiveRecord::Associations::Association

Instance Attribute Details

#saved_change_to_target?Boolean (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb', line 20

def saved_change_to_target?
  super || owner.saved_change_to_attribute?(reflection.foreign_type)
end

#target_changed?Boolean (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb', line 12

def target_changed?
  super || owner.attribute_changed?(reflection.foreign_type)
end

#target_previously_changed?Boolean (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb', line 16

def target_previously_changed?
  super || owner.attribute_previously_changed?(reflection.foreign_type)
end

Instance Method Details

#inverse_reflection_for(record) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb', line 35

def inverse_reflection_for(record)
  reflection.polymorphic_inverse_of(record.class)
end

#klass

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb', line 7

def klass
  type = owner[reflection.foreign_type]
  type.presence && owner.class.polymorphic_class_for(type)
end

#raise_on_type_mismatch!(record) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb', line 39

def raise_on_type_mismatch!(record)
  # A polymorphic association cannot have a type mismatch, by definition
end

#replace_keys(record, force: false) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb', line 25

def replace_keys(record, force: false)
  super

  target_type = record ? record.class.polymorphic_name : nil

  if force || owner._read_attribute(reflection.foreign_type) != target_type
    owner[reflection.foreign_type] = target_type
  end
end

#stale_state (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb', line 43

def stale_state
  foreign_key = super
  foreign_key && [foreign_key.to_s, owner[reflection.foreign_type].to_s]
end