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

Instance Method Details

#call(target: self.target) ⇒ Object



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

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_table, join_keys)

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

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



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

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



44
45
46
# File 'lib/rom/sql/associations/many_to_many.rb', line 44

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.



54
55
56
57
# File 'lib/rom/sql/associations/many_to_many.rb', line 54

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

#target_attrObject



49
50
51
# File 'lib/rom/sql/associations/many_to_many.rb', line 49

def target_attr
  join_relation[target_key].qualified
end