Class: ROM::SQL::Association::ManyToOne

Inherits:
ROM::SQL::Association show all
Defined in:
lib/rom/sql/association/many_to_one.rb

Instance Method Summary collapse

Instance Method Details

#associate(relations, child, parent) ⇒ 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.



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

def associate(relations, child, parent)
  fk, pk = join_key_map(relations)
  child.merge(fk => parent.fetch(pk))
end

#call(relations, left = relations[target.relation]) ⇒ Object



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

def call(relations, left = relations[target.relation])
  right = relations[source.relation]

  left_pk = left.primary_key
  right_fk = left.foreign_key(source.relation)

  left_schema = left.schema
  right_schema = right.schema.project_pk

  schema =
    if left.schema.key?(right_fk)
      left_schema
    else
      left_schema.merge(right_schema.project_fk(left_pk => right_fk))
    end.qualified

  relation = left.inner_join(source_table, join_keys(relations))

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

#combine_keys(relations) ⇒ Object



34
35
36
# File 'lib/rom/sql/association/many_to_one.rb', line 34

def combine_keys(relations)
  Hash[*with_keys(relations)]
end

#join_keys(relations) ⇒ Object



39
40
41
42
43
# File 'lib/rom/sql/association/many_to_one.rb', line 39

def join_keys(relations)
  with_keys(relations) { |source_key, target_key|
    { qualify(source_alias, source_key) => qualify(target, target_key) }
  }
end