123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Associations::HasOneThroughAssociation

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

Overview

Active Record Has One Through Association

Class Method Summary

Association - Inherited

Instance Attribute Summary

ThroughAssociation - Included

ForeignAssociation - Included

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

ThroughAssociation - Included

#build_record,
#construct_join_attributes

Construct attributes for :through pointing to owner and associate.

#ensure_mutable, #ensure_not_nested,
#stale_state

Note: this does not capture all cases, for example it would be impractical to try to properly support stale-checking for nested associations.

#target_scope

We merge in these scopes for two reasons:

#through_association, #through_reflection, #transaction

HasOneAssociation - Inherited

#delete, #handle_dependency, #_create_record, #nullify_owner_attributes, #remove_target!, #replace,
#set_new_record

The reason that the save param for replace is false, if for create (not just build), is because the setting of the foreign keys is actually handled by the scoping when the record is instantiated, and so they are set straight away and do not need to be updated within replace.

#transaction_if

ForeignAssociation - Included

#nullified_owner_attributes,
#set_owner_attributes

Sets the owner attributes on the given record.

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 Method Details

#create_through_record(record, save) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/has_one_through_association.rb', line 15

def create_through_record(record, save)
  ensure_not_nested

  through_proxy  = through_association
  through_record = through_proxy.load_target

  if through_record && !record
    through_record.destroy
  elsif record
    attributes = construct_join_attributes(record)

    if through_record && through_record.destroyed?
      through_record = through_proxy.tap(&:reload).target
    end

    if through_record
      if through_record.new_record?
        through_record.assign_attributes(attributes)
      else
        through_record.update(attributes)
      end
    elsif owner.new_record? || !save
      through_proxy.build(attributes)
    else
      through_proxy.create(attributes)
    end
  end
end

#replace(record, save = true) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/has_one_through_association.rb', line 10

def replace(record, save = true)
  create_through_record(record, save)
  self.target = record
end