Class: ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
- Inherits:
-
JoinBase
- Object
- JoinBase
- ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
- Defined in:
- lib/active_record/associations.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#aliased_join_table_name ⇒ Object
readonly
Returns the value of attribute aliased_join_table_name.
-
#aliased_prefix ⇒ Object
readonly
Returns the value of attribute aliased_prefix.
-
#aliased_table_name ⇒ Object
readonly
Returns the value of attribute aliased_table_name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#parent_table_name ⇒ Object
readonly
Returns the value of attribute parent_table_name.
-
#reflection ⇒ Object
readonly
Returns the value of attribute reflection.
Attributes inherited from JoinBase
Instance Method Summary collapse
- #association_join ⇒ Object
-
#initialize(reflection, join_dependency, parent = nil) ⇒ JoinAssociation
constructor
A new instance of JoinAssociation.
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.
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 |
# File 'lib/active_record/associations.rb', line 2015 def initialize(reflection, join_dependency, parent = nil) reflection.check_validity! if reflection.[: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.[:join_table], "_join") end if [:has_many, :has_one].include?(reflection.macro) && reflection.[:through] @aliased_join_table_name = aliased_table_name_for(reflection.through_reflection.klass.table_name, "_join") end end |
Instance Attribute Details
#aliased_join_table_name ⇒ Object (readonly)
Returns the value of attribute aliased_join_table_name.
2012 2013 2014 |
# File 'lib/active_record/associations.rb', line 2012 def aliased_join_table_name @aliased_join_table_name end |
#aliased_prefix ⇒ Object (readonly)
Returns the value of attribute aliased_prefix.
2012 2013 2014 |
# File 'lib/active_record/associations.rb', line 2012 def aliased_prefix @aliased_prefix end |
#aliased_table_name ⇒ Object (readonly)
Returns the value of attribute aliased_table_name.
2012 2013 2014 |
# File 'lib/active_record/associations.rb', line 2012 def aliased_table_name @aliased_table_name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
2012 2013 2014 |
# File 'lib/active_record/associations.rb', line 2012 def parent @parent end |
#parent_table_name ⇒ Object (readonly)
Returns the value of attribute parent_table_name.
2012 2013 2014 |
# File 'lib/active_record/associations.rb', line 2012 def parent_table_name @parent_table_name end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
2012 2013 2014 |
# File 'lib/active_record/associations.rb', line 2012 def reflection @reflection end |
Instance Method Details
#association_join ⇒ Object
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 |
# File 'lib/active_record/associations.rb', line 2038 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([:join_table], aliased_join_table_name), connection.quote_table_name(aliased_join_table_name), [: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), [:association_foreign_key] || klass.to_s.foreign_key ] when :has_many, :has_one case when reflection.[:through] through_conditions = through_reflection.[:conditions] ? "AND #{interpolate_sql(sanitize_sql(through_reflection.[:conditions]))}" : '' jt_foreign_key = jt_as_extra = jt_source_extra = jt_sti_extra = nil first_key = second_key = as_extra = nil if through_reflection.[:as] # has_many :through against a polymorphic join jt_foreign_key = through_reflection.[: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.[: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.[:as] first_key = "#{source_reflection.[:as]}_id" second_key = [:foreign_key] || primary_key as_extra = " AND %s.%s = %s" % [ connection.quote_table_name(aliased_table_name), connection.quote_column_name("#{source_reflection.[: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 = [: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.[: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.[:foreign_type]), klass.quote_value(reflection.[: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.[: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.[:as]}_id", connection.quote_table_name(parent.aliased_table_name), parent.primary_key, connection.quote_table_name(aliased_table_name), "#{reflection.[:as]}_type", klass.quote_value(parent.active_record.base_class.name) ] else foreign_key = [: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), [: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.[:conditions], aliased_table_name))} " if ref && ref.[:conditions] end join end |