class Dynamoid::Associations::BelongsTo

@private

Public Instance Methods

associate(hash_key) click to toggle source

Override default implementation to handle case when we store id as scalar value, not as collection

# File lib/dynamoid/associations/belongs_to.rb, line 25
def associate(hash_key)
  target.send(target_association).disassociate(source.hash_key) if target && target_association

  if options[:foreign_key]
    source.update_attribute(source_attribute, hash_key)
  else
    source.update_attribute(source_attribute, Set[hash_key])
  end
end
declaration_field_name() click to toggle source
# File lib/dynamoid/associations/belongs_to.rb, line 11
def declaration_field_name
  options[:foreign_key] || "#{name}_ids"
end
declaration_field_type() click to toggle source
# File lib/dynamoid/associations/belongs_to.rb, line 15
def declaration_field_type
  if options[:foreign_key]
    target_class.attributes[target_class.hash_key][:type]
  else
    :set
  end
end

Private Instance Methods

target_association() click to toggle source

Find the target association, either has_many or has_one. Uses either options or the source class name and default parsing to return the most likely name for the target association.

@since 0.2.0

# File lib/dynamoid/associations/belongs_to.rb, line 41
def target_association
  has_many_key_name = options[:inverse_of] || source.class.to_s.underscore.pluralize.to_sym
  has_one_key_name = options[:inverse_of] || source.class.to_s.underscore.to_sym
  unless target_class.associations[has_many_key_name].nil?
    return has_many_key_name if target_class.associations[has_many_key_name][:type] == :has_many
  end

  unless target_class.associations[has_one_key_name].nil?
    return has_one_key_name if target_class.associations[has_one_key_name][:type] == :has_one
  end
end