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.



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
1589
1590
1591
1592
1593
# File 'lib/active_record/associations.rb', line 1555

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.



1552
1553
1554
# File 'lib/active_record/associations.rb', line 1552

def aliased_join_table_name
  @aliased_join_table_name
end

#aliased_prefixObject (readonly)

Returns the value of attribute aliased_prefix.



1552
1553
1554
# File 'lib/active_record/associations.rb', line 1552

def aliased_prefix
  @aliased_prefix
end

#aliased_table_nameObject (readonly)

Returns the value of attribute aliased_table_name.



1552
1553
1554
# File 'lib/active_record/associations.rb', line 1552

def aliased_table_name
  @aliased_table_name
end

#parentObject (readonly)

Returns the value of attribute parent.



1552
1553
1554
# File 'lib/active_record/associations.rb', line 1552

def parent
  @parent
end

#parent_table_nameObject (readonly)

Returns the value of attribute parent_table_name.



1552
1553
1554
# File 'lib/active_record/associations.rb', line 1552

def parent_table_name
  @parent_table_name
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



1552
1553
1554
# File 'lib/active_record/associations.rb', line 1552

def reflection
  @reflection
end

Instance Method Details

#association_joinObject



1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
# File 'lib/active_record/associations.rb', line 1595

def association_join
  connection = reflection.active_record.connection
  join = case reflection.macro
    when :has_and_belongs_to_many
      " #{join_type} %s ON %s.%s = %s.%s " % [
         table_alias_for(options[:join_table], aliased_join_table_name),
         connection.quote_table_name(aliased_join_table_name),
         options[:foreign_key] || reflection.active_record.to_s.foreign_key,
         connection.quote_table_name(parent.aliased_table_name),
		     reflection.active_record.primary_key] +
      " #{join_type} %s ON %s.%s = %s.%s " % [
         table_name_and_alias,
		     connection.quote_table_name(aliased_table_name),
		     klass.primary_key,
         connection.quote_table_name(aliased_join_table_name),
		     options[:association_foreign_key] || klass.to_s.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]))}" : ''
          
          jt_foreign_key = jt_as_extra = jt_source_extra = jt_sti_extra = nil 
          first_key = second_key = as_extra = nil 
          
          if through_reflection.options[:as] # has_many :through against a polymorphic join
            jt_foreign_key = through_reflection.options[:as].to_s + '_id'
            jt_as_extra = " AND %s.%s = %s" % [
              connection.quote_table_name(aliased_join_table_name),
              connection.quote_column_name(through_reflection.options[:as].to_s + '_type'),
              klass.quote_value(parent.active_record.base_class.name)
            ]
          else
            jt_foreign_key = through_reflection.primary_key_name 
          end
            
          case source_reflection.macro
          when :has_many
            if source_reflection.options[:as] 
              first_key   = "#{source_reflection.options[:as]}_id" 
              second_key  = options[:foreign_key] || primary_key 
              as_extra    = " AND %s.%s = %s" % [
                connection.quote_table_name(aliased_table_name),
                connection.quote_column_name("#{source_reflection.options[:as]}_type"),
                klass.quote_value(source_reflection.active_record.base_class.name) 
              ]
            else
              first_key   = through_reflection.klass.base_class.to_s.foreign_key
              second_key  = options[:foreign_key] || primary_key
            end
            
            unless through_reflection.klass.descends_from_active_record?
              jt_sti_extra = " AND %s.%s = %s" % [
                connection.quote_table_name(aliased_join_table_name),
                connection.quote_column_name(through_reflection.active_record.inheritance_column),
                through_reflection.klass.quote_value(through_reflection.klass.name.demodulize)]
            end
          when :belongs_to
            first_key = primary_key
            if reflection.options[:source_type]
              second_key = source_reflection.association_foreign_key
              jt_source_extra = " AND %s.%s = %s" % [
                connection.quote_table_name(aliased_join_table_name),
                connection.quote_column_name(reflection.source_reflection.options[:foreign_type]),
                klass.quote_value(reflection.options[:source_type])
              ]
            else
              second_key = source_reflection.primary_key_name
            end
          end

          " #{join_type} %s ON (%s.%s = %s.%s%s%s%s) " % [
            table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
            connection.quote_table_name(parent.aliased_table_name),
			connection.quote_column_name(parent.primary_key),
            connection.quote_table_name(aliased_join_table_name),
			connection.quote_column_name(jt_foreign_key),
            jt_as_extra, jt_source_extra, jt_sti_extra
          ] +
          " #{join_type} %s ON (%s.%s = %s.%s%s) " % [
            table_name_and_alias, 
            connection.quote_table_name(aliased_table_name),
			connection.quote_column_name(first_key),
            connection.quote_table_name(aliased_join_table_name),
			connection.quote_column_name(second_key),
            as_extra
          ]

        when reflection.options[:as] && [:has_many, :has_one].include?(reflection.macro)
          " #{join_type} %s ON %s.%s = %s.%s AND %s.%s = %s" % [
            table_name_and_alias,
            connection.quote_table_name(aliased_table_name),
			"#{reflection.options[:as]}_id",
            connection.quote_table_name(parent.aliased_table_name),
			parent.primary_key,
            connection.quote_table_name(aliased_table_name),
			"#{reflection.options[:as]}_type",
            klass.quote_value(parent.active_record.base_class.name)
          ]
        else
          foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
          " #{join_type} %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
      " #{join_type} %s ON %s.%s = %s.%s " % [
         table_name_and_alias,
		     connection.quote_table_name(aliased_table_name),
		     reflection.klass.primary_key,
         connection.quote_table_name(parent.aliased_table_name),
		     options[:foreign_key] || klass.to_s.foreign_key
        ]
    else
      ""
  end || ''
  join << %(AND %s.%s = %s ) % [
    connection.quote_table_name(aliased_table_name),
    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