Class: ActiveRecord::Associations::ClassMethods::JoinAssociation
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::ClassMethods::JoinAssociation
- Defined in:
- lib/composite_primary_keys/through_association_scope.rb
Instance Method Summary collapse
- #association_join ⇒ Object
-
#composite_join_predicates(table1, keys1, table2, keys2) ⇒ Object
Ugly to include this twice, but I couldn’t figure out how to make this work via a module.
Instance Method Details
#association_join ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/composite_primary_keys/through_association_scope.rb', line 121 def association_join return @join if @join aliased_table = Arel::Table.new(table_name, :as => @aliased_table_name, :engine => arel_engine) parent_table = Arel::Table.new(parent.table_name, :as => parent.aliased_table_name, :engine => arel_engine) @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_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 = join_table[through_reflection.[:as].to_s + '_type'].eq(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 = 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[parent.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 # CPK # [aliased_table[foreign_key].eq(parent_table[reflection.options[:primary_key] || parent.primary_key])] composite_join_predicates(aliased_table, foreign_key, 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.cpk_primary_key])] end unless klass.descends_from_active_record? sti_column = aliased_table[klass.inheritance_column] sti_condition = sti_column.eq(klass.sti_name) klass.send(:subclasses).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 << interpolate_sql(sanitize_sql(ref.[:conditions], aliased_table_name)) end end @join end |
#composite_join_predicates(table1, keys1, table2, keys2) ⇒ Object
Ugly to include this twice, but I couldn’t figure out how to make this work via a module
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/composite_primary_keys/through_association_scope.rb', line 107 def composite_join_predicates(table1, keys1, table2, keys2) attributes1 = [keys1].flatten.map do |key| table1[key] end attributes2 = [keys2].flatten.map do |key| table2[key] end [attributes1, attributes2].transpose.map do |attribute1, attribute2| attribute1.eq(attribute2) end end |