Module: Babik::QuerySet::Join

Defined in:
lib/babik/queryset/lib/join/join.rb,
lib/babik/queryset/lib/join/association_joiner.rb

Overview

Join between two tables

Defined Under Namespace

Classes: AbstractJoin, AssociationJoiner, LeftJoin, OriginTable, TargetTable

Class Method Summary collapse

Class Method Details

.new_from_association(association, association_position, origin_table_alias, join = LeftJoin) ⇒ LeftJoin

Construct a new Join from an association

Parameters:

  • association

    Association between two ActiveRecord::Base objects.

  • association_position

    Association position. Used when the relationship is a many-to-many through.

  • origin_table_alias

    Alias of table that is the origin of the join.

  • join (LeftJoin) (defaults to: LeftJoin)

    Join class.

Returns:

  • (LeftJoin)

    object with the join for this association.



14
15
16
17
18
19
20
21
22
23
# File 'lib/babik/queryset/lib/join/join.rb', line 14

def self.new_from_association(association, association_position, origin_table_alias, join = LeftJoin)
  owner_table = association.active_record.table_name
  target_table_alias = "#{owner_table}__#{association.name}_#{association_position}"
  join_keys = association.join_keys

  target_table = TargetTable.new(association.table_name, target_table_alias, join_keys.key)
  origin_table = OriginTable.new(origin_table_alias, join_keys.foreign_key)

  join.new(target_table, origin_table)
end