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?, #setter

Methods included from Association

#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



24
25
26
27
28
29
30
31
32
# File 'lib/dynamoid/associations/belongs_to.rb', line 24

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



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

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

#declaration_field_typeObject



14
15
16
17
18
19
20
# File 'lib/dynamoid/associations/belongs_to.rb', line 14

def declaration_field_type
  if options[:foreign_key]
    target_class.attributes[target_class.hash_key][:type]
  else
    :set
  end
end