class DatastaxRails::Associations::BelongsToAssociation

belongs_to associations are the child side of a parent/child relationship

class Car < DatastaxRails::Base
  uuid :id
  uuid :owner_id
  belongs_to :owner, class_name: 'Person'
end

Valid options:

Attributes

updated[R]
updated?[R]

Public Instance Methods

replace(record) click to toggle source
# File lib/datastax_rails/associations/belongs_to_association.rb, line 17
def replace(record)
  raise_on_type_mismatch(record) if record

  replace_keys(record)
  set_inverse_instance(record)

  @updated = true if record

  self.target = record
end

Private Instance Methods

different_target?(record) click to toggle source

Checks whether record is different to the current target, without loading it

# File lib/datastax_rails/associations/belongs_to_association.rb, line 35
def different_target?(record)
  record.nil? && owner[reflection.foreign_key] ||
    record && record.id != owner[reflection.foreign_key]
end
find_target?() click to toggle source
# File lib/datastax_rails/associations/belongs_to_association.rb, line 30
def find_target?
  !loaded? && foreign_key_present? && klass
end
foreign_key_present?() click to toggle source
# File lib/datastax_rails/associations/belongs_to_association.rb, line 50
def foreign_key_present?
  owner[reflection.foreign_key]
end
invertible_for?(record) click to toggle source

NOTE - for now, we're only supporting inverse setting from belongs_to back onto has_one associations.

# File lib/datastax_rails/associations/belongs_to_association.rb, line 56
def invertible_for?(record)
  inverse = inverse_reflection_for(record)
  inverse && inverse.macro == :has_one
end
replace_keys(record) click to toggle source
# File lib/datastax_rails/associations/belongs_to_association.rb, line 40
def replace_keys(record)
  owner.loaded_attributes[reflection.foreign_key] = true
  owner.send("#{reflection.foreign_key}_will_change!")
  if record
    owner[reflection.foreign_key] = record.id
  else
    owner[reflection.foreign_key] = nil
  end
end
stale_state() click to toggle source
# File lib/datastax_rails/associations/belongs_to_association.rb, line 69
def stale_state
  owner[reflection.foreign_key].to_s
end
target_id() click to toggle source
# File lib/datastax_rails/associations/belongs_to_association.rb, line 61
def target_id
  if options[:primary_key]
    owner.send(reflection.name).try(:id)
  else
    owner[reflection.foreign_key]
  end
end