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

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

Direct Known Subclasses

OneToOneThrough

Instance Method Summary collapse

Methods included from Core

#preload

Instance Method Details

#call(target: self.target) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rom/sql/associations/many_to_many.rb', line 11

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.project(*columns)
      end
    else
      target_schema
    end.qualified

  relation = left.join(source.name.dataset, join_keys)

  if view
    apply_view(schema, relation)
  else
    schema.(relation)
  end
end

#join(type, source = self.source, target = self.target) ⇒ Object



35
36
37
38
39
# File 'lib/rom/sql/associations/many_to_many.rb', line 35

def join(type, source = self.source, target = self.target)
  through_assoc = source.associations[through]
  joined = through_assoc.join(type, source)
  joined.__send__(type, target.name.dataset, join_keys).qualified
end

#join_keysObject



42
43
44
# File 'lib/rom/sql/associations/many_to_many.rb', line 42

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.



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

def persist(children, parents)
  join_tuples = associate(children, parents)
  join_relation.multi_insert(join_tuples)
end

#source_attrObject



47
48
49
# File 'lib/rom/sql/associations/many_to_many.rb', line 47

def source_attr
  source[source_key].qualified
end

#target_attrObject



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

def target_attr
  join_relation[target_key].qualified
end