Class: ROM::SQL::Associations::ManyToMany

Inherits:
Associations::ManyToMany
  • Object
show all
Includes:
Core, SelfRef
Defined in:
lib/rom/sql/associations/many_to_many.rb

Direct Known Subclasses

OneToOneThrough

Instance Method Summary collapse

Methods included from SelfRef

included, #source_attr

Methods included from Core

#preload, #wrapped

Instance Method Details

#call(target: self.target) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# 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_keysObject



52
53
54
# File 'lib/rom/sql/associations/many_to_many.rb', line 52

def join_keys
  { source_attr => target_attr }
end

#persist(children, parents) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
65
# 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_attrObject



57
58
59
# File 'lib/rom/sql/associations/many_to_many.rb', line 57

def target_attr
  join_relation[target_key].qualified
end