method

join_constraints

Importance_0
v5.1.7 - Show latest stable - 0 notes - Class: JoinAssociation
join_constraints(foreign_table, foreign_klass, join_type, tables, chain) public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/associations/join_dependency/join_association.rb, line 26
        def join_constraints(foreign_table, foreign_klass, join_type, tables, chain)
          joins         = []
          binds         = []
          tables        = tables.reverse

          # The chain starts with the target table, but we want to end with it here (makes
          # more sense in this context), so we reverse
          chain.reverse_each do |reflection|
            table = tables.shift
            klass = reflection.klass

            join_keys   = reflection.join_keys
            key         = join_keys.key
            foreign_key = join_keys.foreign_key

            constraint = build_constraint(klass, table, key, foreign_table, foreign_key)

            rel = reflection.join_scope(table)

            if rel && !rel.arel.constraints.empty?
              binds += rel.bound_attributes
              constraint = constraint.and rel.arel.constraints
            end

            if reflection.type
              value = foreign_klass.base_class.name
              column = klass.columns_hash[reflection.type.to_s]

              binds << Relation::QueryAttribute.new(column.name, value, klass.type_for_attribute(column.name))
              constraint = constraint.and klass.arel_attribute(reflection.type, table).eq(Arel::Nodes::BindParam.new)
            end

            joins << table.create_join(table, table.create_on(constraint), join_type)

            # The current table in this iteration becomes the foreign table in the next
            foreign_table, foreign_klass = table, klass
          end

          JoinInformation.new joins, binds
        end
Register or log in to add new notes.