Class: ActiveRecord::Associations::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_primary_keys/associations/association.rb

Instance Method Summary collapse

Instance Method Details

#creation_attributesObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/composite_primary_keys/associations/association.rb', line 4

def creation_attributes
  attributes = {}

  if (reflection.has_one? || reflection.collection?) && !options[:through]
    # CPK
    # attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
    Array(reflection.foreign_key).zip(Array(reflection.active_record_primary_key)).each do |key1, key2|
      attributes[key1] = owner[key2]
    end

    if reflection.options[:as]
      attributes[reflection.type] = owner.class.base_class.name
    end
  end

  attributes
end