Class: ROM::SQL::Associations::ManyToMany
- Inherits:
-
Associations::ManyToMany
- Object
- Associations::ManyToMany
- ROM::SQL::Associations::ManyToMany
- Includes:
- SelfRef
- Defined in:
- lib/rom/sql/associations/many_to_many.rb
Direct Known Subclasses
Instance Method Summary collapse
- #call(target: self.target) ⇒ Object
- #join(type, source = self.source, target = self.target) ⇒ Object
- #join_keys ⇒ Object
- #source_attr ⇒ Object included from SelfRef
- #target_attr ⇒ Object
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.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_keys ⇒ Object
52 53 54 |
# File 'lib/rom/sql/associations/many_to_many.rb', line 52 def join_keys { source_attr => target_attr } end |
#source_attr ⇒ Object Originally defined in module SelfRef
#target_attr ⇒ Object
57 58 59 |
# File 'lib/rom/sql/associations/many_to_many.rb', line 57 def target_attr join_relation[target_key].qualified end |