Class: ActiveRecord::Associations::HasOneAssociation

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 a hash.



56
57
58
59
# File 'lib/portable_model/active_record.rb', line 56

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

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

Import the association from a hash.

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/portable_model/active_record.rb', line 63

def import_portable_association(record_hash, options = {})
  NotPortableError.raise_on_not_portable(self)
  raise ArgumentError.new('specified argument is not a hash') unless record_hash.is_a?(Hash)
  raise 'cannot replace existing association record' unless target.nil?

  proxy_reflection.klass.start_importing do
    proxy_owner.transaction do
      record_hash.merge!(primary_key_hash)
      assoc_record = proxy_reflection.klass.import_from_hash(record_hash, options)
      replace(assoc_record)
    end
  end
end