Flowdock
method

join_to

Importance_0
v3.1.0 - Show latest stable - 0 notes - Class: JoinAssociation
join_to(relation) 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 62
        def join_to(relation)
          tables        = @tables.dup
          foreign_table = parent_table
          foreign_klass = parent.active_record

          # 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_with_index do |reflection, i|
            table = tables.shift

            case reflection.source_macro
            when :belongs_to
              key         = reflection.association_primary_key
              foreign_key = reflection.foreign_key
            when :has_and_belongs_to_many
              # Join the join table first...
              relation.from(join(
                table,
                table[reflection.foreign_key].
                  eq(foreign_table[reflection.active_record_primary_key])
              ))

              foreign_table, table = table, tables.shift

              key         = reflection.association_primary_key
              foreign_key = reflection.association_foreign_key
            else
              key         = reflection.foreign_key
              foreign_key = reflection.active_record_primary_key
            end

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

            conditions = self.conditions[i].dup
            conditions << { reflection.type => foreign_klass.base_class.name } if reflection.type

            unless conditions.empty?
              constraint = constraint.and(sanitize(conditions, table))
            end

            relation.from(join(table, constraint))

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

          relation
        end
Register or log in to add new notes.