123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Associations::SingularAssociation

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Association
Instance Chain:
self, Association
Inherits: ActiveRecord::Associations::Association
Defined in: activerecord/lib/active_record/associations/singular_association.rb

Class Method Summary

Association - Inherited

Instance Attribute Summary

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

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_record(attributes, raise_error = false, &block) (private)

Raises:

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 56

def _create_record(attributes, raise_error = false, &block)
  record = build_record(attributes, &block)
  saved = record.save
  set_new_record(record)
  raise RecordInvalid.new(record) if !saved && raise_error
  record
end

#build(attributes = nil, &block)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 22

def build(attributes = nil, &block)
  record = build_record(attributes, &block)
  set_new_record(record)
  record
end

#find_target (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 40

def find_target
  if disable_joins
    scope.first
  else
    super.first
  end
end

#force_reload_reader

Implements the reload reader method, e.g. foo.reload_bar for Foo.has_one :bar

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 30

def force_reload_reader
  reload(true)
  target
end

#reader

Implements the reader method, e.g. foo.bar for Foo.has_one :bar

[ GitHub ]

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

def reader
  ensure_klass_exists!

  if !loaded? || stale_target?
    reload
  end

  target
end

#replace(record) (private)

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 48

def replace(record)
  raise NotImplementedError, "Subclasses must implement a replace(record) method"
end

#scope_for_create (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 36

def scope_for_create
  super.except!(*Array(klass.primary_key))
end

#set_new_record(record) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 52

def set_new_record(record)
  replace(record)
end

#writer(record)

Implements the writer method, e.g. foo.bar= for Foo.belongs_to :bar

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 18

def writer(record)
  replace(record)
end