diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-05-27 10:29:59 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-05-27 10:29:59 -0700 |
commit | 85bfb08c1ad74454ddf17b60d2d5ae3429b87d2b (patch) | |
tree | d5e69cbfa8cca81e2fc3109335768cff16370051 /activerecord/lib | |
parent | 52434e9a512477d632b492793bcae5b4732ea689 (diff) | |
parent | c917fb7c35c8ef0f48963f44059e7088430bb2d0 (diff) | |
download | rails-85bfb08c1ad74454ddf17b60d2d5ae3429b87d2b.tar.gz rails-85bfb08c1ad74454ddf17b60d2d5ae3429b87d2b.tar.bz2 rails-85bfb08c1ad74454ddf17b60d2d5ae3429b87d2b.zip |
Merge pull request #14834 from al2o3cr/issue14155
Correctly alias table names when joining more than once
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/alias_tracker.rb | 12 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/finder_methods.rb | 11 |
2 files changed, 21 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/alias_tracker.rb b/activerecord/lib/active_record/associations/alias_tracker.rb index 85109aee6c..a6a1947148 100644 --- a/activerecord/lib/active_record/associations/alias_tracker.rb +++ b/activerecord/lib/active_record/associations/alias_tracker.rb @@ -32,8 +32,18 @@ module ActiveRecord join.left.downcase.scan( /join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/ ).size - else + elsif join.respond_to? :left join.left.table_name == name ? 1 : 0 + else + # this branch is reached by two tests: + # + # activerecord/test/cases/associations/cascaded_eager_loading_test.rb:37 + # with :posts + # + # activerecord/test/cases/associations/eager_test.rb:1133 + # with :comments + # + 0 end end diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index db32ae12a8..47e90e9021 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -336,7 +336,16 @@ module ActiveRecord end def find_with_associations - join_dependency = construct_join_dependency + # NOTE: the JoinDependency constructed here needs to know about + # any joins already present in `self`, so pass them in + # + # failing to do so means that in cases like activerecord/test/cases/associations/inner_join_association_test.rb:136 + # incorrect SQL is generated. In that case, the join dependency for + # SpecialCategorizations is constructed without knowledge of the + # preexisting join in joins_values to categorizations (by way of + # the `has_many :through` for categories). + # + join_dependency = construct_join_dependency(joins_values) aliases = join_dependency.aliases relation = select aliases.columns |