class ActiveRecord::Associations::BelongsToAssociation

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/active_record/associations/belongs_to_association.rbs

class ActiveRecord::Associations::BelongsToAssociation < ActiveRecord::Associations::SingularAssociation
  def foreign_key_present?: () -> untyped
  def invertible_for?: (User record) -> untyped
  def primary_key: (Class klass) -> String
  def reset: () -> untyped
  def stale_state: () -> untyped
end

:nodoc:
= Active Record Belongs To Association

def decrement_counters

def decrement_counters
  update_counters(-1)
end

def decrement_counters_before_last_save

def decrement_counters_before_last_save
  if reflection.polymorphic?
    model_type_was = owner.attribute_before_last_save(reflection.foreign_type)
    model_was = owner.class.polymorphic_class_for(model_type_was) if model_type_was
  else
    model_was = klass
  end
  foreign_key_was = owner.attribute_before_last_save(reflection.foreign_key)
  if foreign_key_was && model_was < ActiveRecord::Base
    update_counters_via_scope(model_was, foreign_key_was, -1)
  end
end

def default(&block)

def default(&block)
  writer(owner.instance_exec(&block)) if reader.nil?
end

def find_target?

def find_target?
  !loaded? && foreign_key_present? && klass
end

def foreign_key_present?

Experimental RBS support (using type sampling data from the type_fusion project).

def foreign_key_present?: () -> untyped

This signature was generated using 1 sample from 1 application.

def foreign_key_present?
  owner._read_attribute(reflection.foreign_key)
end

def handle_dependency

:nodoc:
= Active Record Belongs To Association
def handle_dependency
  return unless load_target
  case options[:dependent]
  when :destroy
    raise ActiveRecord::Rollback unless target.destroy
  when :destroy_async
    id = owner.public_send(reflection.foreign_key.to_sym)
    primary_key_column = reflection.active_record_primary_key.to_sym
    enqueue_destroy_association(
      owner_model_name: owner.class.to_s,
      owner_id: owner.id,
      association_class: reflection.klass.to_s,
      association_ids: [id],
      association_primary_key_column: primary_key_column,
      ensuring_owner_was_method: options.fetch(:ensuring_owner_was, nil)
    )
  else
    target.public_send(options[:dependent])
  end
end

def increment_counters

def increment_counters
  update_counters(1)
end

def inversed_from(record)

def inversed_from(record)
  replace_keys(record)
  super
end

def invertible_for?(record)

Experimental RBS support (using type sampling data from the type_fusion project).

def invertible_for?: (User record) -> untyped

This signature was generated using 1 sample from 1 application.

def invertible_for?(record)
  inverse = inverse_reflection_for(record)
  inverse && (inverse.has_one? || inverse.klass.has_many_inversing)
end

def primary_key(klass)

Experimental RBS support (using type sampling data from the type_fusion project).

def primary_key: (Class klass) -> String

This signature was generated using 1 sample from 1 application.

def primary_key(klass)
  reflection.association_primary_key(klass)
end

def replace(record)

def replace(record)
  if record
    raise_on_type_mismatch!(record)
    set_inverse_instance(record)
    @updated = true
  elsif target
    remove_inverse_instance(target)
  end
  replace_keys(record, force: true)
  self.target = record
end

def replace_keys(record, force: false)

def replace_keys(record, force: false)
  target_key = record ? record._read_attribute(primary_key(record.class)) : nil
  if force || owner._read_attribute(reflection.foreign_key) != target_key
    owner[reflection.foreign_key] = target_key
  end
end

def require_counter_update?

def require_counter_update?
  reflection.counter_cache_column && owner.persisted?
end

def reset

Experimental RBS support (using type sampling data from the type_fusion project).

def reset: () -> untyped

This signature was generated using 1 sample from 1 application.

def reset
  super
  @updated = false
end

def saved_change_to_target?

def saved_change_to_target?
  owner.saved_change_to_attribute?(reflection.foreign_key)
end

def stale_state

Experimental RBS support (using type sampling data from the type_fusion project).

def stale_state: () -> untyped

This signature was generated using 1 sample from 1 application.

def stale_state
  result = owner._read_attribute(reflection.foreign_key) { |n| owner.send(:missing_attribute, n, caller) }
  result && result.to_s
end

def target_changed?

def target_changed?
  owner.attribute_changed?(reflection.foreign_key) || (!foreign_key_present? && target&.new_record?)
end

def target_previously_changed?

def target_previously_changed?
  owner.attribute_previously_changed?(reflection.foreign_key)
end

def update_counters(by)

def update_counters(by)
  if require_counter_update? && foreign_key_present?
    if target && !stale_target?
      target.increment!(reflection.counter_cache_column, by, touch: reflection.options[:touch])
    else
      update_counters_via_scope(klass, owner._read_attribute(reflection.foreign_key), by)
    end
  end
end

def update_counters_via_scope(klass, foreign_key, by)

def update_counters_via_scope(klass, foreign_key, by)
  scope = klass.unscoped.where!(primary_key(klass) => foreign_key)
  scope.update_counters(reflection.counter_cache_column => by, touch: reflection.options[:touch])
end

def updated?

def updated?
  @updated
end