module ROM::SQL::Plugin::Associates::InstanceMethods

Public Instance Methods

associate(tuples, curried_parent = nil, assoc:, keys:, parent: curried_parent) click to toggle source

Set fk on tuples from parent tuple

@param [Array<Hash>, Hash] tuples The input tuple(s) @param [Hash] parent The parent tuple with its pk already set

@return [Array<Hash>]

@api public

# File lib/rom/sql/plugin/associates.rb, line 112
def associate(tuples, curried_parent = nil, assoc:, keys:, parent: curried_parent)
  result_type = result

  output_tuples =
    case assoc
    when SQL::Associations::ManyToMany
      result_type = tuples.is_a?(Array) ? :many : :one

      assoc.persist(tuples, parent)

      pk, fk = assoc.parent_combine_keys

      case parent
      when Array
        parent.flat_map do |p|
          tuples.map { |tuple| Hash(tuple).merge(fk => p[pk]) }
        end
      else
        tuples.map { |tuple| Hash(tuple).update(fk => parent[pk]) }
      end
    else
      with_input_tuples(tuples).map { |tuple|
        assoc.associate(tuple, parent)
      }
    end

  result_type == :one ? output_tuples[0] : output_tuples
end
with_association(name, opts = EMPTY_HASH) click to toggle source

Return a new command with the provided association

@param [Symbol, Relation::Name] name The name of the association

@return [Command]

@api public

# File lib/rom/sql/plugin/associates.rb, line 148
def with_association(name, opts = EMPTY_HASH)
  self.class.build(
    relation,
    **options, associations: associations.merge(name => opts)
  )
end