diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-01-29 05:43:45 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-02-01 06:27:23 +0900 |
commit | 111ccc832bc977b15af12c14e7ca078dad2d4373 (patch) | |
tree | 640fa3c6f1b6d2553f689399e57873ef11a06bed /activerecord/lib | |
parent | a57b5292b0987c62d8567b253c9b54dc84b560d6 (diff) | |
download | rails-111ccc832bc977b15af12c14e7ca078dad2d4373.tar.gz rails-111ccc832bc977b15af12c14e7ca078dad2d4373.tar.bz2 rails-111ccc832bc977b15af12c14e7ca078dad2d4373.zip |
Chain scope constraints should respect own table alias
Fixes #27666.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/association_scope.rb | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index c6d204d3c2..badde9973f 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -128,9 +128,9 @@ module ActiveRecord reflection = chain_head while reflection table = reflection.alias_name + next_reflection = reflection.next unless reflection == chain_tail - next_reflection = reflection.next foreign_table = next_reflection.alias_name scope = next_chain_scope(scope, table, reflection, association_klass, foreign_table, next_reflection) end @@ -138,7 +138,7 @@ module ActiveRecord # Exclude the scope of the association itself, because that # was already merged in the #scope method. reflection.constraints.each do |scope_chain_item| - item = eval_scope(reflection.klass, scope_chain_item, owner) + item = eval_scope(reflection.klass, table, scope_chain_item, owner) if scope_chain_item == refl.scope scope.merge! item.except(:where, :includes) @@ -153,14 +153,15 @@ module ActiveRecord scope.order_values |= item.order_values end - reflection = reflection.next + reflection = next_reflection end scope end - def eval_scope(klass, scope, owner) - klass.unscoped.instance_exec(owner, &scope) + def eval_scope(klass, table, scope, owner) + predicate_builder = PredicateBuilder.new(TableMetadata.new(klass, table)) + ActiveRecord::Relation.create(klass, table, predicate_builder).instance_exec(owner, &scope) end end end |