Class: Dynamoid::Associations::BelongsTo

Inherits:
Object
  • Object
show all
Includes:
SingleAssociation
Defined in:
lib/dynamoid/associations/belongs_to.rb

Instance Attribute Summary

Attributes included from Association

#loaded, #name, #options, #source

Instance Method Summary collapse

Methods included from SingleAssociation

#==, #create, #create!, #delete, #disassociate, #empty?, #method_missing, #nil?, #respond_to_missing?, #setter

Methods included from Association

#disassociate_source, #find_target, #initialize, #loaded?, #reset, #target

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dynamoid::Associations::SingleAssociation

Instance Method Details

#associate(hash_key) ⇒ Object

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



25
26
27
28
29
30
31
32
33
# 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_nameObject



11
12
13
# File 'lib/dynamoid/associations/belongs_to.rb', line 11

def declaration_field_name
  options[:foreign_key] || "#{name}_ids"
end

#declaration_field_typeObject



15
16
17
18
19
20
21
# 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