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.



1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
# File 'lib/active_record/associations.rb', line 1800

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

  super(reflection.klass)
  @join_dependency    = join_dependency
  @parent             = parent
  @reflection         = reflection
  @aliased_prefix     = "t#{ join_dependency.joins.size }"
  @parent_table_name  = parent.active_record.table_name
  @aliased_table_name = aliased_table_name_for(table_name)
  
  if reflection.macro == :has_and_belongs_to_many
    @aliased_join_table_name = aliased_table_name_for(reflection.options[:join_table], "_join")
  end
        
  if [:has_many, :has_one].include?(reflection.macro) && reflection.options[:through]
    @aliased_join_table_name = aliased_table_name_for(reflection.through_reflection.klass.table_name, "_join")
  end
end

Instance Attribute Details

#aliased_join_table_nameObject (readonly)

Returns the value of attribute aliased_join_table_name.



1797
1798
1799
# File 'lib/active_record/associations.rb', line 1797

def aliased_join_table_name
  @aliased_join_table_name
end

#aliased_prefixObject (readonly)

Returns the value of attribute aliased_prefix.



1797
1798
1799
# File 'lib/active_record/associations.rb', line 1797

def aliased_prefix
  @aliased_prefix
end

#aliased_table_nameObject (readonly)

Returns the value of attribute aliased_table_name.



1797
1798
1799
# File 'lib/active_record/associations.rb', line 1797

def aliased_table_name
  @aliased_table_name
end

#parentObject (readonly)

Returns the value of attribute parent.



1797
1798
1799
# File 'lib/active_record/associations.rb', line 1797

def parent
  @parent
end

#parent_table_nameObject (readonly)

Returns the value of attribute parent_table_name.



1797
1798
1799
# File 'lib/active_record/associations.rb', line 1797

def parent_table_name
  @parent_table_name
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



1797
1798
1799
# File 'lib/active_record/associations.rb', line 1797

def reflection
  @reflection
end

Instance Method Details

#association_joinObject



1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
# File 'lib/active_record/associations.rb', line 1823

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.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.sti_name)]
            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] || reflection.primary_key_name
        ]
    else
      ""
  end || ''
  join << %(AND %s) % [
    klass.send(:type_condition, aliased_table_name)] 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