aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-12-07 16:48:59 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2010-12-07 16:48:59 -0800
commit2d9d6cd4c263f639adf8e26c7d95e7772e0c4eb7 (patch)
tree77f3d650bc01bbd0983a1bf7b0231ad70dd629ee
parentd6133209ce54bac430e63ec512251eae182b79e1 (diff)
downloadrails-2d9d6cd4c263f639adf8e26c7d95e7772e0c4eb7.tar.gz
rails-2d9d6cd4c263f639adf8e26c7d95e7772e0c4eb7.tar.bz2
rails-2d9d6cd4c263f639adf8e26c7d95e7772e0c4eb7.zip
passing the ast to JoinDependency
-rw-r--r--activerecord/lib/active_record/associations/class_methods/join_dependency.rb13
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
2 files changed, 12 insertions, 3 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 34928bc764..86d43f21d3 100644
--- a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb
+++ b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb
@@ -45,11 +45,20 @@ module ActiveRecord
end
def count_aliases_from_table_joins(name)
- return 0 unless @table_joins
+ return 0 if !@table_joins || Arel::Table === @table_joins
+ 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)
+ }.sum
+ end
+
+ def count_aliases_from_string(join_sql, name)
# quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase
quoted_name = active_record.connection.quote_table_name(name.downcase).downcase
- join_sql = @table_joins.downcase
# Table names
join_sql.scan(/join(?:\s+\w+)?\s+#{quoted_name}\son/).size +
# Table aliases
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 51ba17c9be..906ad7699c 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -196,7 +196,7 @@ module ActiveRecord
def construct_relation_for_association_calculations
including = (@eager_load_values + @includes_values).uniq
- join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, arel.join_sql)
+ join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, arel.froms.first)
relation = except(:includes, :eager_load, :preload)
apply_join_dependency(relation, join_dependency)
end