    # File lib/active_record/associations.rb, line 1441
                def association_join
                  join = case reflection.macro
                    when :has_and_belongs_to_many
                      " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
                         table_alias_for(options[:join_table], aliased_join_table_name),
                         aliased_join_table_name,
                         options[:foreign_key] || reflection.active_record.to_s.classify.foreign_key,
                         reflection.active_record.table_name, reflection.active_record.primary_key] +
                      " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
                         table_name_and_alias, aliased_table_name, klass.primary_key,
                         aliased_join_table_name, options[:association_foreign_key] || klass.table_name.classify.foreign_key
                         ]
                    when :has_many, :has_one
                      case
                        when reflection.macro == :has_many && reflection.options[:through]
                          through_conditions = through_reflection.options[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.options[:conditions]))}" : ''
                          if through_reflection.options[:as] # has_many :through against a polymorphic join
                            polymorphic_foreign_key  = through_reflection.options[:as].to_s + '_id'
                            polymorphic_foreign_type = through_reflection.options[:as].to_s + '_type'

                            " LEFT OUTER JOIN %s ON (%s.%s = %s.%s AND %s.%s = %s) "  % [
                              table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
                              aliased_join_table_name, polymorphic_foreign_key,
                              parent.aliased_table_name, parent.primary_key,
                              aliased_join_table_name, polymorphic_foreign_type, klass.quote(parent.active_record.base_class.name)] +
                            " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [table_name_and_alias,
                              aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key
                            ]
                          else
                            if source_reflection.macro == :has_many && source_reflection.options[:as]
                              " LEFT OUTER JOIN %s ON %s.%s = %s.%s "  % [
                                table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name,
                                through_reflection.primary_key_name,
                                parent.aliased_table_name, parent.primary_key] +
                              " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [
                                table_name_and_alias,
                                aliased_table_name, "#{source_reflection.options[:as]}_id",
                                aliased_join_table_name, options[:foreign_key] || primary_key,
                                aliased_table_name, "#{source_reflection.options[:as]}_type",
                                klass.quote(source_reflection.active_record.base_class.name)
                              ]
                            else
                              case source_reflection.macro
                                when :belongs_to
                                  first_key  = primary_key
                                  second_key = options[:foreign_key] || klass.to_s.classify.foreign_key
                                when :has_many
                                  first_key  = through_reflection.klass.to_s.classify.foreign_key
                                  second_key = options[:foreign_key] || primary_key
                              end

                              " LEFT OUTER JOIN %s ON %s.%s = %s.%s "  % [
                                table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name,
                                through_reflection.primary_key_name,
                                parent.aliased_table_name, parent.primary_key] +
                              " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
                                table_name_and_alias,
                                aliased_table_name, first_key,
                                aliased_join_table_name, second_key
                              ]
                            end
                          end

                        when reflection.macro == :has_many && reflection.options[:as]
                          " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s" % [
                            table_name_and_alias,
                            aliased_table_name, "#{reflection.options[:as]}_id",
                            parent.aliased_table_name, parent.primary_key,
                            aliased_table_name, "#{reflection.options[:as]}_type",
                            klass.quote(parent.active_record.base_class.name)
                          ]
                        when reflection.macro == :has_one && reflection.options[:as]
                          " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [
                            table_name_and_alias,
                            aliased_table_name, "#{reflection.options[:as]}_id",
                            parent.aliased_table_name, parent.primary_key,
                            aliased_table_name, "#{reflection.options[:as]}_type",
                            klass.quote(reflection.active_record.base_class.name)
                          ]
                        else
                          foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
                          " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
                            table_name_and_alias,
                            aliased_table_name, foreign_key,
                            parent.aliased_table_name, parent.primary_key
                          ]
                      end
                    when :belongs_to
                      " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
                         table_name_and_alias, aliased_table_name, reflection.klass.primary_key,
                         parent.aliased_table_name, options[:foreign_key] || klass.to_s.foreign_key
                        ]
                    else
                      ""
                  end || ''
                  join << %(AND %s.%s = %s ) % [
                    aliased_table_name,
                    reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column),
                    klass.quote(klass.name)] unless klass.descends_from_active_record?
                  join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions]
                  join
                end