From 33609678f7554bfdb655299a197b86b52f877069 Mon Sep 17 00:00:00 2001 From: James Le Cuirot Date: Sun, 13 Sep 2009 23:57:37 +0100 Subject: [PATCH] Detect joined tables a little more accurately so that included associations are still joined when they should be. This addresses #2631. --- activerecord/lib/active_record/associations.rb | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index c739fdd..5ea0195 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1701,6 +1701,11 @@ module ActiveRecord string.scan(/([\.a-zA-Z_]+).?\./).flatten end + def joined_tables_in_string(string) + return [] if string.blank? + string.scan(/\bJOIN\s+["`]?([\.a-zA-Z_]+)["`]?\s/i).flatten + end + def tables_in_hash(hash) return [] if hash.blank? tables = hash.map do |key, value| @@ -1745,13 +1750,13 @@ module ActiveRecord [table_name] + case merged_joins when Symbol, Hash, Array if array_of_strings?(merged_joins) - tables_in_string(merged_joins.join(' ')) + joined_tables_in_string(merged_joins.join(' ')) else join_dependency = ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, merged_joins, nil) join_dependency.join_associations.collect {|join_association| [join_association.aliased_join_table_name, join_association.aliased_table_name]}.flatten.compact end else - tables_in_string(merged_joins) + joined_tables_in_string(merged_joins) end end -- 1.6.3.1