Class: ActiveRecord::Associations::HasManyAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/portable_model/active_record.rb

Instance Method Summary collapse

Instance Method Details

#export_portable_associationObject

Export the association to an array of hashes.



83
84
85
86
# File 'lib/portable_model/active_record.rb', line 83

def export_portable_association
  NotPortableError.raise_on_not_portable(self)
  proxy_reflection.klass.start_exporting { map(&:export_to_hash) }
end

#import_portable_association(record_hashes, options = {}) ⇒ Object

Import the association from an array of hashes.

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/portable_model/active_record.rb', line 90

def import_portable_association(record_hashes, options = {})
  NotPortableError.raise_on_not_portable(self)
  raise ArgumentError.new('specified argument is not an array of hashes') unless record_hashes.is_a?(Array) && record_hashes.all? { |record_hash| record_hash.is_a?(Hash) }

  proxy_reflection.klass.start_importing do
    proxy_owner.transaction do
      delete_all
      assoc_records = record_hashes.map do |record_hash|
        record_hash.merge!(primary_key_hash)
        proxy_reflection.klass.import_from_hash(record_hash, options)
      end
      replace(assoc_records)
    end
  end
end