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.
-
#join_type ⇒ Object
What type of join will be generated, either Arel::InnerJoin (default) or Arel::OuterJoin.
-
#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
- #==(other) ⇒ Object
- #association_join ⇒ Object
- #find_parent_in(other_join_dependency) ⇒ Object
-
#initialize(reflection, join_dependency, parent = nil) ⇒ JoinAssociation
constructor
A new instance of JoinAssociation.
- #join_relation(joining_relation) ⇒ Object
- #relation ⇒ Object
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.
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 |
# File 'lib/active_record/associations.rb', line 2100 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) @join = nil @join_type = Arel::InnerJoin 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.
2095 2096 2097 |
# File 'lib/active_record/associations.rb', line 2095 def aliased_join_table_name @aliased_join_table_name end |
#aliased_prefix ⇒ Object (readonly)
Returns the value of attribute aliased_prefix.
2095 2096 2097 |
# File 'lib/active_record/associations.rb', line 2095 def aliased_prefix @aliased_prefix end |
#aliased_table_name ⇒ Object (readonly)
Returns the value of attribute aliased_table_name.
2095 2096 2097 |
# File 'lib/active_record/associations.rb', line 2095 def aliased_table_name @aliased_table_name end |
#join_type ⇒ Object
What type of join will be generated, either Arel::InnerJoin (default) or Arel::OuterJoin
2097 2098 2099 |
# File 'lib/active_record/associations.rb', line 2097 def join_type @join_type end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
2095 2096 2097 |
# File 'lib/active_record/associations.rb', line 2095 def parent @parent end |
#parent_table_name ⇒ Object (readonly)
Returns the value of attribute parent_table_name.
2095 2096 2097 |
# File 'lib/active_record/associations.rb', line 2095 def parent_table_name @parent_table_name end |
#reflection ⇒ Object (readonly)
Returns the value of attribute reflection.
2095 2096 2097 |
# File 'lib/active_record/associations.rb', line 2095 def reflection @reflection end |
Instance Method Details
#==(other) ⇒ Object
2125 2126 2127 2128 2129 |
# File 'lib/active_record/associations.rb', line 2125 def ==(other) other.class == self.class && other.reflection == reflection && other.parent == parent end |
#association_join ⇒ Object
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 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 |
# File 'lib/active_record/associations.rb', line 2137 def association_join return @join if @join aliased_table = Arel::Table.new(table_name, :as => @aliased_table_name, :engine => arel_engine, :columns => klass.columns) parent_table = Arel::Table.new(parent.table_name, :as => parent.aliased_table_name, :engine => arel_engine, :columns => parent.active_record.columns) @join = case reflection.macro when :has_and_belongs_to_many join_table = Arel::Table.new([:join_table], :as => aliased_join_table_name, :engine => arel_engine) fk = [:foreign_key] || reflection.active_record.to_s.foreign_key klass_fk = [:association_foreign_key] || klass.to_s.foreign_key [ join_table[fk].eq(parent_table[reflection.active_record.primary_key]), aliased_table[klass.primary_key].eq(join_table[klass_fk]) ] when :has_many, :has_one if reflection.[:through] join_table = Arel::Table.new(through_reflection.klass.table_name, :as => aliased_join_table_name, :engine => arel_engine) jt_as_extra = jt_source_extra = jt_sti_extra = nil first_key = second_key = as_extra = nil if through_reflection.macro == :belongs_to jt_primary_key = through_reflection.primary_key_name jt_foreign_key = through_reflection.association_primary_key else jt_primary_key = through_reflection.active_record_primary_key jt_foreign_key = through_reflection.primary_key_name if through_reflection.[:as] # has_many :through against a polymorphic join jt_as_extra = join_table[through_reflection.[:as].to_s + '_type'].eq(parent.active_record.base_class.name) end 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 = aliased_table["#{source_reflection.[:as]}_type"].eq(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 = join_table[through_reflection.active_record.inheritance_column].eq(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 = join_table[reflection.source_reflection.[:foreign_type]].eq(reflection.[:source_type]) else second_key = source_reflection.primary_key_name end end [ [parent_table[jt_primary_key].eq(join_table[jt_foreign_key]), jt_as_extra, jt_source_extra, jt_sti_extra].reject{|x| x.blank? }, aliased_table[first_key].eq(join_table[second_key]) ] elsif reflection.[:as] id_rel = aliased_table["#{reflection.[:as]}_id"].eq(parent_table[parent.primary_key]) type_rel = aliased_table["#{reflection.[:as]}_type"].eq(parent.active_record.base_class.name) [id_rel, type_rel] else foreign_key = [:foreign_key] || reflection.active_record.name.foreign_key [aliased_table[foreign_key].eq(parent_table[reflection.[:primary_key] || parent.primary_key])] end when :belongs_to [aliased_table[[:primary_key] || reflection.klass.primary_key].eq(parent_table[[:foreign_key] || reflection.primary_key_name])] end unless klass.descends_from_active_record? sti_column = aliased_table[klass.inheritance_column] sti_condition = sti_column.eq(klass.sti_name) klass.descendants.each {|subclass| sti_condition = sti_condition.or(sti_column.eq(subclass.sti_name)) } @join << sti_condition end [through_reflection, reflection].each do |ref| if ref && ref.[:conditions] @join << process_conditions(ref.[:conditions], aliased_table_name) end end @join end |
#find_parent_in(other_join_dependency) ⇒ Object
2131 2132 2133 2134 2135 |
# File 'lib/active_record/associations.rb', line 2131 def find_parent_in(other_join_dependency) other_join_dependency.joins.detect do |join| self.parent == join end end |
#join_relation(joining_relation) ⇒ Object
2247 2248 2249 2250 |
# File 'lib/active_record/associations.rb', line 2247 def join_relation(joining_relation) self.join_type = Arel::OuterJoin joining_relation.joins(self) end |
#relation ⇒ Object
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 |
# File 'lib/active_record/associations.rb', line 2233 def relation aliased = Arel::Table.new(table_name, :as => @aliased_table_name, :engine => arel_engine, :columns => klass.columns) if reflection.macro == :has_and_belongs_to_many [Arel::Table.new([:join_table], :as => aliased_join_table_name, :engine => arel_engine), aliased] elsif reflection.[:through] [Arel::Table.new(through_reflection.klass.table_name, :as => aliased_join_table_name, :engine => arel_engine), aliased] else aliased end end |