Class: ActiveRecord::Associations::BelongsToPolymorphicAssociation
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
ActiveRecord::Associations::BelongsToAssociation
|
Defined in: | activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb |
Overview
Active Record Belongs To Polymorphic Association
Class Method Summary
Association
- Inherited
Instance Attribute Summary
- #saved_change_to_target? ⇒ Boolean readonly
- #target_changed? ⇒ Boolean readonly
- #target_previously_changed? ⇒ Boolean readonly
BelongsToAssociation
- Inherited
#saved_change_to_target?, #target_changed?, #target_previously_changed?, #updated?, #find_target?, #foreign_key_present?, #require_counter_update? |
Association
- Inherited
#collection? | Whether the association represent a single record or a collection of records. |
#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 |
#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
- #klass
- #inverse_reflection_for(record) private
- #raise_on_type_mismatch!(record) private
- #replace_keys(record, force: false) private
- #stale_state private
BelongsToAssociation
- Inherited
SingularAssociation
- Inherited
#build, | |
#force_reload_reader | Implements the reload reader method, e.g. |
#reader | Implements the reader method, e.g. |
#reset | Resets the loaded flag to |
#writer | Implements the writer method, e.g. |
#_create_record, #find_target, #replace, #scope_for_create, #set_new_record |
Association
- Inherited
#async_load_target, #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 |
#marshal_dump | We can’t dump @reflection and @through_reflection since it contains the scope proc. |
#marshal_load, | |
#reload | Reloads the target and returns |
#remove_inverse_instance | Remove the inverse association, if possible. |
#reset | Resets the loaded flag to |
#reset_negative_cache, #reset_scope, #scope, | |
#set_inverse_instance | Set the inverse association, if possible. |
#set_inverse_instance_from_queries, #set_strict_loading, | |
#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. |
#find_target, | |
#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 |
#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 |
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 if foreign_key = super [foreign_key, owner[reflection.foreign_type]] end end