lib/rom/plugins/relation/sql/auto_combine.rb in rom-sql-0.7.0 vs lib/rom/plugins/relation/sql/auto_combine.rb in rom-sql-0.8.0

- old
+ new

@@ -14,10 +14,11 @@ module ClassInterface def inherited(klass) super klass.auto_curry :for_combine + klass.auto_curry :preload end end module InstanceInterface # Default methods for fetching combined relation @@ -25,12 +26,24 @@ # This method is used by default by `combine` # # @return [SQL::Relation] # # @api private - def for_combine(keys, relation) - pk, fk = keys.to_a.flatten - where(fk => relation.map { |tuple| tuple[pk] }) + def for_combine(spec) + source_key, target_key, target = + case spec + when ROM::SQL::Association + [*spec.join_keys(__registry__).flatten, spec.call(__registry__)] + else + [*spec.flatten, self] + end + + target.preload(source_key, target_key) + end + + # @api private + def preload(source_key, target_key, source) + where(target_key => source.pluck(source_key.to_sym)) end end end end end