From d98cb5153dad97cde82ec91a1993043cfa9bc0e4 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 Dec 2010 17:05:15 -0800 Subject: JoinDependency is always created with an AST now --- .../associations/class_methods/join_dependency.rb | 14 ++++++++++---- activerecord/lib/active_record/relation/query_methods.rb | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb index 86d43f21d3..928e8152a5 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb @@ -49,10 +49,16 @@ module ActiveRecord return count_aliases_from_string(@table_joins.downcase, name) if String === @table_joins - @table_joins.grep(Arel::Table).find_all { |table| - table.name.downcase == name - }.length + @table_joins.grep(String).map { |s| - count_aliases_from_string(s, name) + @table_joins.grep(Arel::Nodes::Join).map { |join| + right = join.right + case right + when Arel::Table + right.name.downcase == name ? 1 : 0 + when String + count_aliases_from_string(right.downcase, name) + else + 0 + end }.sum end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 2933d427f0..194ce881e0 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -185,6 +185,20 @@ module ActiveRecord arel.join_sql end + def custom_join_ast(joins) + joins = joins.reject { |join| join.blank? } + + return if joins.empty? + + @implicit_readonly = true + + head = table.create_string_join(table, joins.shift) + + joins.inject(head) do |ast, join| + ast.right = table.create_string_join(ast.right, join) + end + end + def build_arel arel = table @@ -256,7 +270,7 @@ module ActiveRecord non_association_joins = (joins - association_joins - stashed_association_joins) custom_joins = custom_join_sql(non_association_joins) - join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, custom_joins) + join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, custom_join_ast(non_association_joins)) join_dependency.graft(*stashed_association_joins) -- cgit v1.2.3