Module: ROM::SQL::Plugin::Associates::InstanceMethods Private

Defined in:
lib/rom/sql/plugin/associates.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#associate(tuples, curried_parent = nil, assoc:, keys:, parent: curried_parent) ⇒ Array<Hash>

Set fk on tuples from parent tuple

Parameters:

  • tuples (Array<Hash>, Hash)

    The input tuple(s)

  • parent (Hash) (defaults to: curried_parent)

    The parent tuple with its pk already set

Returns:

  • (Array<Hash>)

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Command

Return a new command with the provided association

Parameters:

  • name (Symbol, Relation::Name)

    The name of the association

Returns:

  • (Command)

148
149
150
151
152
153
# 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