class ROM::Associations::ManyToMany
Abstract
many-to-many association type
@api public
Attributes
@!attribute [r] join_relation
@return [Relation] Intermediate join relation
Public Class Methods
@api private
ROM::Associations::Abstract::new
# File lib/rom/associations/many_to_many.rb, line 17 def initialize(*) super @join_relation = relations[through] end
Public Instance Methods
Associate child tuples with the provided parent
@param [Array<Hash>] children An array with child tuples @param [Array,Hash] parent An array with parent tuples or a single tuple
@return [Array<Hash>]
@api private
# File lib/rom/associations/many_to_many.rb, line 67 def associate(children, parent) ((spk, sfk), (tfk, tpk)) = join_key_map case parent when Array parent.map { |p| associate(children, p) }.flatten(1) else children.map { |tuple| { sfk => tuple.fetch(spk), tfk => parent.fetch(tpk) } } end end
Adapters should implement this method
@abstract
@api public
# File lib/rom/associations/many_to_many.rb, line 28 def call(*) raise NotImplementedError end
Return configured or inferred FK name
@return [Symbol]
@api public
# File lib/rom/associations/many_to_many.rb, line 37 def foreign_key definition.foreign_key || join_relation.foreign_key(source.name) end
Return parent's relation combine keys
@return [Hash<Symbol=>Symbol>]
@api private
# File lib/rom/associations/many_to_many.rb, line 55 def parent_combine_keys target.associations[source.name].combine_keys.to_a.flatten(1) end
Return join-relation name
@return [Symbol]
@api public
# File lib/rom/associations/many_to_many.rb, line 46 def through definition.through end
Protected Instance Methods
Return association for many-to-many-through
@return [Association]
@api protected
# File lib/rom/associations/many_to_many.rb, line 105 def join_assoc if join_relation.associations.key?(through.assoc_name) join_relation.associations[through.assoc_name] else join_relation.associations[through.target] end end
Return a [pk, fk] mapping for source/target relations
@return [Array<Symbol>]
@api protected
ROM::Associations::Abstract#join_key_map
# File lib/rom/associations/many_to_many.rb, line 118 def join_key_map left = super right = join_assoc.join_key_map [left, right] end
Primary key name on the source side
@return [Symbol]
@api protected
# File lib/rom/associations/many_to_many.rb, line 87 def source_key source.primary_key end
Foreign key name on the target side
@return [Symbol]
@api protected
# File lib/rom/associations/many_to_many.rb, line 96 def target_key foreign_key end