lib/rom/sql/plugin/associates.rb in rom-sql-1.0.0.rc1 vs lib/rom/sql/plugin/associates.rb in rom-sql-1.0.0.rc2
- old
+ new
@@ -43,36 +43,45 @@
#
# @return [Array<Hash>]
#
# @api public
def associate(tuples, parent, assoc:, keys:)
- input_tuples =
+ result_type = result
+
+ output_tuples =
case assoc
when Symbol
fk, pk = keys
with_input_tuples(tuples).map { |tuple|
tuple.merge(fk => parent.fetch(pk))
}
when Association::ManyToMany
+ result_type = tuples.is_a?(Array) ? :many : :one
+
join_tuples = assoc.associate(__registry__, tuples, parent)
join_relation = assoc.join_relation(__registry__)
join_relation.multi_insert(join_tuples)
pk, fk = __registry__[assoc.target]
.associations[assoc.source]
.combine_keys(__registry__).to_a.flatten
- pk_extend = { fk => parent[pk] }
-
- tuples.map { |tuple| tuple.update(pk_extend) }
+ case parent
+ when Array
+ parent.map do |p|
+ tuples.map { |tuple| tuple.merge(fk => p[pk]) }
+ end.flatten(1)
+ else
+ tuples.map { |tuple| Hash(tuple).update(fk => parent[pk]) }
+ end
when Association
with_input_tuples(tuples).map { |tuple|
assoc.associate(relation.__registry__, tuple, parent)
}
end
- one? ? input_tuples[0] : input_tuples
+ result_type == :one ? output_tuples[0] : output_tuples
end
# @api public
def with_association(name, opts = EMPTY_HASH)
self.class.build(