Method: ROM::SQL::Relation::Writing#import

Defined in:
lib/rom/sql/relation/writing.rb

#import(other_sql_relation, options) ⇒ Integer #import(other, options) ⇒ Integer

Insert tuples from other relation

NOTE: The method implicitly uses a transaction

Examples:

users.import(new_users)

Overloads:

  • #import(other_sql_relation, options) ⇒ Integer

    If both relations uses the same gateway the INSERT … SELECT statement will be used for importing the data

    Parameters:

  • #import(other, options) ⇒ Integer

    Import data from another relation. The source relation will be materialized before loading

    Parameters:

Returns:

  • (Integer)

    Number of imported tuples



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rom/sql/relation/writing.rb', line 115

def import(other, options = EMPTY_HASH)
  columns = other.schema.map { |a| a.alias || a.name }

  if other.gateway.eql?(gateway)
    dataset.import(columns, other.dataset, options)
  else
    keys = columns.map(&:to_sym)
    dataset.import(
      columns,
      other.to_a.map { |record|
        record.to_h.values_at(*keys)
      },
      options
    )
  end
end