class ROM::SQL::Associations::ManyToMany

Public Instance Methods

call(target: self.target) click to toggle source

@api public

# File lib/rom/sql/associations/many_to_many.rb, line 15
def call(target: self.target)
  left = join_assoc.(target: target)

  schema =
    if left.schema.key?(foreign_key)
      if target != self.target
        target.schema.merge(join_schema)
      else
        left.schema.uniq.project(*columns)
      end
    else
      target_schema
    end.qualified

  relation = left.join(source_table, join_keys)

  if view
    apply_view(schema, relation)
  else
    schema.(relation)
  end
end
join(type, source = self.source, target = self.target) click to toggle source

@api public

# File lib/rom/sql/associations/many_to_many.rb, line 39
def join(type, source = self.source, target = self.target)
  through_assoc = source.associations[through]

  # first we join source to intermediary
  joined = through_assoc.join(type, source)

  # then we join intermediary to target
  target_ds  = target.name.dataset
  through_jk = through_assoc.target.associations[target_ds].join_keys
  joined.__send__(type, target_ds, through_jk).qualified
end
join_keys() click to toggle source

@api public

# File lib/rom/sql/associations/many_to_many.rb, line 52
def join_keys
  { source_attr => target_attr }
end
persist(children, parents) click to toggle source

@api private

# File lib/rom/sql/associations/many_to_many.rb, line 62
def persist(children, parents)
  join_tuples = associate(children, parents)
  join_relation.multi_insert(join_tuples)
end
target_attr() click to toggle source

@api public

# File lib/rom/sql/associations/many_to_many.rb, line 57
def target_attr
  join_relation[target_key].qualified
end

Private Instance Methods

columns() click to toggle source

@api private

# File lib/rom/sql/associations/many_to_many.rb, line 80
def columns
  target_schema.map(&:name)
end
join_schema() click to toggle source

@api private

# File lib/rom/sql/associations/many_to_many.rb, line 75
def join_schema
  join_relation.schema.project(foreign_key)
end
target_schema() click to toggle source

@api private

# File lib/rom/sql/associations/many_to_many.rb, line 70
def target_schema
  target.schema.merge(join_schema)
end