Class: ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation

Inherits:
JoinBase
  • Object
show all
Defined in:
lib/active_record/associations.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from JoinBase

#active_record, #table_joins

Instance Method Summary collapse

Methods inherited from JoinBase

#aliased_primary_key, #column_names_with_alias, #extract_record, #instantiate, #record_id

Constructor Details

#initialize(reflection, join_dependency, parent = nil) ⇒ JoinAssociation

Returns a new instance of JoinAssociation.



1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
# File 'lib/active_record/associations.rb', line 1438

def initialize(reflection, join_dependency, parent = nil)
  reflection.check_validity!
  if reflection.options[:polymorphic]
    raise EagerLoadPolymorphicError.new(reflection)
  end

  super(reflection.klass)
  @parent             = parent
  @reflection         = reflection
  @aliased_prefix     = "t#{ join_dependency.joins.size }"
  @aliased_table_name = table_name #.tr('.', '_') # start with the table name, sub out any .'s
  @parent_table_name  = parent.active_record.table_name

  if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{aliased_table_name.downcase}\son}
    join_dependency.table_aliases[aliased_table_name] += 1
  end
  
  unless join_dependency.table_aliases[aliased_table_name].zero?
    # if the table name has been used, then use an alias
    @aliased_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}"
    table_index = join_dependency.table_aliases[aliased_table_name]
    join_dependency.table_aliases[aliased_table_name] += 1
    @aliased_table_name = @aliased_table_name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
  else
    join_dependency.table_aliases[aliased_table_name] += 1
  end
  
  if reflection.macro == :has_and_belongs_to_many || (reflection.macro == :has_many && reflection.options[:through])
    @aliased_join_table_name = reflection.macro == :has_and_belongs_to_many ? reflection.options[:join_table] : reflection.through_reflection.klass.table_name
    unless join_dependency.table_aliases[aliased_join_table_name].zero?
      @aliased_join_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}_join"
      table_index = join_dependency.table_aliases[aliased_join_table_name]
      join_dependency.table_aliases[aliased_join_table_name] += 1
      @aliased_join_table_name = @aliased_join_table_name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
    else
      join_dependency.table_aliases[aliased_join_table_name] += 1
    end
  end
end

Instance Attribute Details

#aliased_join_table_nameObject (readonly)

Returns the value of attribute aliased_join_table_name.



1435
1436
1437
# File 'lib/active_record/associations.rb', line 1435

def aliased_join_table_name
  @aliased_join_table_name
end

#aliased_prefixObject (readonly)

Returns the value of attribute aliased_prefix.



1435
1436
1437
# File 'lib/active_record/associations.rb', line 1435

def aliased_prefix
  @aliased_prefix
end

#aliased_table_nameObject (readonly)

Returns the value of attribute aliased_table_name.



1435
1436
1437
# File 'lib/active_record/associations.rb', line 1435

def aliased_table_name
  @aliased_table_name
end

#parentObject (readonly)

Returns the value of attribute parent.



1435
1436
1437
# File 'lib/active_record/associations.rb', line 1435

def parent
  @parent
end

#parent_table_nameObject (readonly)

Returns the value of attribute parent_table_name.



1435
1436
1437
# File 'lib/active_record/associations.rb', line 1435

def parent_table_name
  @parent_table_name
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



1435
1436
1437
# File 'lib/active_record/associations.rb', line 1435

def reflection
  @reflection
end

Instance Method Details

#association_joinObject



1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
# File 'lib/active_record/associations.rb', line 1478

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,
         parent.aliased_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_value(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_value(source_reflection.active_record.base_class.name)
              ]
            else
              case source_reflection.macro
                when :belongs_to
                  first_key  = primary_key
                  second_key = source_reflection.options[:foreign_key] || klass.to_s.classify.foreign_key
                  extra      = nil
                when :has_many
                  first_key  = through_reflection.klass.base_class.to_s.classify.foreign_key
                  second_key = options[:foreign_key] || primary_key
                  extra      = through_reflection.klass.descends_from_active_record? ? nil :
                    " AND %s.%s = %s" % [
                      aliased_join_table_name,
                      reflection.active_record.connection.quote_column_name(through_reflection.active_record.inheritance_column),
                      through_reflection.klass.quote_value(through_reflection.klass.name.demodulize)]
              end
              " LEFT OUTER JOIN %s ON (%s.%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, extra] +
              " 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_value(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_value(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(klass.inheritance_column), 
    klass.quote_value(klass.name.demodulize)] unless klass.descends_from_active_record?

  [through_reflection, reflection].each do |ref|
    join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions]))} " if ref && ref.options[:conditions]
  end

  join
end