Class: ROM::SQL::Associations::ManyToOne

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

Instance Method Summary collapse

Methods included from SelfRef

included, #join_keys, #source_attr, #target_attr

Methods included from Core

#preload, #wrapped

Instance Method Details

#call(target: self.target, preload: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rom/sql/associations/many_to_one.rb', line 15

def call(target: self.target, preload: false)
  if preload
    schema = target.schema.qualified
    relation = target
  else
    right = source

    target_pk = target.schema.primary_key_name
    right_fk = target.foreign_key(source.name)

    target_schema = target.schema
    right_schema = right.schema.project_pk

    schema =
      if target.schema.key?(right_fk)
        target_schema
      else
        target_schema.merge(right_schema.project_fk(target_pk => right_fk))
      end.qualified

    relation = target.join(source_table, join_keys)
  end

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

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



46
47
48
# File 'lib/rom/sql/associations/many_to_one.rb', line 46

def join(type, source = self.source, target = self.target)
  source.__send__(type, target.name.dataset, join_keys).qualified
end

#prepare(target) ⇒ 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.



51
52
53
# File 'lib/rom/sql/associations/many_to_one.rb', line 51

def prepare(target)
  call(target: target, preload: true)
end