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
-
#associate(tuples, curried_parent = nil, assoc:, keys:, parent: curried_parent) ⇒ Array<Hash>
Set fk on tuples from parent tuple.
-
#with_association(name, opts = EMPTY_HASH) ⇒ Command
Return a new command with the provided association.
Instance Method Details
#associate(tuples, curried_parent = nil, assoc:, keys:, parent: curried_parent) ⇒ Array<Hash>
Set fk on tuples from parent tuple
rubocop:disable Lint/UnusedMethodArgument
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rom/sql/plugin/associates.rb', line 149 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
186 187 188 189 190 191 |
# File 'lib/rom/sql/plugin/associates.rb', line 186 def with_association(name, opts = EMPTY_HASH) self.class.build( relation, **, associations: associations.merge(name => opts) ) end |