aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-12-14 14:57:03 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2010-12-14 14:57:03 -0800
commit2e7da54f770d1e076005c7670ab8d99431888faa (patch)
tree820351c7258b4d0b4478b9feead1ca9b01d51958
parent6212ecaa0be6c0f035d9be26e8693bd1f09fdfb6 (diff)
downloadrails-2e7da54f770d1e076005c7670ab8d99431888faa.tar.gz
rails-2e7da54f770d1e076005c7670ab8d99431888faa.tar.bz2
rails-2e7da54f770d1e076005c7670ab8d99431888faa.zip
supporting arel AST nodes when building join statements
-rw-r--r--activerecord/lib/active_record/associations/through_association_scope.rb32
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb18
2 files changed, 32 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/associations/through_association_scope.rb b/activerecord/lib/active_record/associations/through_association_scope.rb
index acddfda924..48386ee124 100644
--- a/activerecord/lib/active_record/associations/through_association_scope.rb
+++ b/activerecord/lib/active_record/associations/through_association_scope.rb
@@ -55,33 +55,35 @@ module ActiveRecord
end
def construct_joins(custom_joins = nil)
- polymorphic_join = nil
+ right = @reflection.through_reflection.klass.arel_table
+ left = @reflection.klass.arel_table
+
+ conditions = []
+
if @reflection.source_reflection.macro == :belongs_to
reflection_primary_key = @reflection.klass.primary_key
source_primary_key = @reflection.source_reflection.primary_key_name
if @reflection.options[:source_type]
- polymorphic_join = "AND %s.%s = %s" % [
- @reflection.through_reflection.quoted_table_name, "#{@reflection.source_reflection.options[:foreign_type]}",
- @owner.class.quote_value(@reflection.options[:source_type])
- ]
+ column = @reflection.source_reflection.options[:foreign_type]
+ conditions <<
+ right[column].eq(@reflection.options[:source_type])
end
else
reflection_primary_key = @reflection.source_reflection.primary_key_name
source_primary_key = @reflection.through_reflection.klass.primary_key
if @reflection.source_reflection.options[:as]
- polymorphic_join = "AND %s.%s = %s" % [
- @reflection.quoted_table_name, "#{@reflection.source_reflection.options[:as]}_type",
- @owner.class.quote_value(@reflection.through_reflection.klass.name)
- ]
+ column = "#{@reflection.source_reflection.options[:as]}_type"
+ conditions <<
+ left[column].eq(@reflection.through_reflection.klass.name)
end
end
- "INNER JOIN %s ON %s.%s = %s.%s %s #{@reflection.options[:joins]} #{custom_joins}" % [
- @reflection.through_reflection.quoted_table_name,
- @reflection.quoted_table_name, reflection_primary_key,
- @reflection.through_reflection.quoted_table_name, source_primary_key,
- polymorphic_join
- ]
+ conditions <<
+ left[reflection_primary_key].eq(right[source_primary_key])
+
+ right.create_join(
+ right,
+ right.create_on(right.create_and(conditions)))
end
# Construct attributes for associate pointing to owner.
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index ef14f48a8f..8e50f4a9bc 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -247,6 +247,8 @@ module ActiveRecord
'association_join'
when ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
'stashed_join'
+ when Arel::Nodes::Join
+ 'join_node'
else
raise 'unknown class: %s' % join.class.name
end
@@ -254,12 +256,22 @@ module ActiveRecord
association_joins = buckets['association_join'] || []
stashed_association_joins = buckets['stashed_join'] || []
+ join_nodes = buckets['join_node'] || []
string_joins = (buckets['string_join'] || []).map { |x|
x.strip
}.uniq
+
join_list = custom_join_ast(manager, string_joins)
- join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, join_list)
+ join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(
+ @klass,
+ association_joins,
+ join_list
+ )
+
+ join_nodes.each do |join|
+ join_dependency.table_aliases[join.left.name.downcase] = 1
+ end
join_dependency.graft(*stashed_association_joins)
@@ -270,9 +282,9 @@ module ActiveRecord
association.join_to(manager)
end
- return manager unless join_list
+ manager.join_sources.concat join_nodes
+ manager.join_sources.concat join_list
- join_list.each { |j| manager.from j }
manager
end