diff options
Diffstat (limited to 'activerecord/lib/active_record/associations/alias_tracker.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/alias_tracker.rb | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations/alias_tracker.rb b/activerecord/lib/active_record/associations/alias_tracker.rb index 64582188b6..6428fcec0a 100644 --- a/activerecord/lib/active_record/associations/alias_tracker.rb +++ b/activerecord/lib/active_record/associations/alias_tracker.rb @@ -5,11 +5,10 @@ module ActiveRecord # Keeps track of table aliases for ActiveRecord::Associations::ClassMethods::JoinDependency and # ActiveRecord::Associations::ThroughAssociationScope class AliasTracker # :nodoc: - # other_sql is some other sql which might conflict with the aliases we assign here. Therefore - # we store other_sql so that we can scan it before assigning a specific name. - def initialize(other_sql = nil) - @aliases = Hash.new - @other_sql = other_sql.to_s.downcase + # table_joins is an array of arel joins which might conflict with the aliases we assign here + def initialize(table_joins = nil) + @aliases = Hash.new + @table_joins = table_joins end def aliased_name_for(table_name, aliased_name = nil) @@ -47,15 +46,24 @@ module ActiveRecord def initialize_count_for(name) @aliases[name] = 0 - unless @other_sql.blank? + unless @table_joins.nil? || Arel::Table === @table_joins # quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase - quoted_name = connection.quote_table_name(name.downcase).downcase - - # Table names - @aliases[name] += @other_sql.scan(/join(?:\s+\w+)?\s+#{quoted_name}\son/).size - - # Table aliases - @aliases[name] += @other_sql.scan(/join(?:\s+\w+)?\s+\S+\s+#{quoted_name}\son/).size + quoted_name = connection.quote_table_name(name).downcase + + @aliases[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 + # Table names + table aliases + right.downcase.scan( + /join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/ + ).size + else + 0 + end + }.sum end @aliases[name] |