diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index e954bd2fb7fb4..93f041b2ac547 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,7 +1,13 @@ ## Rails 5.0.0.rc1 (May 06, 2016) ## -* No changes. +* Merge parent joins for through associations. + Merges parent associations joins for through associations into a scope. Previously joins in association scope were + taken into account only for root associations with that scope. + + Fixes #22538. + + *Seva Orlov* ## Rails 5.0.0.beta4 (April 27, 2016) ## diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 4ef353789fe6c..ccf3f077f1e6e 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -158,8 +158,9 @@ def add_constraints(scope, owner, association_klass, refl, chain_head, chain_tai scope end + # Merges inner joins from `other` relation to `scope` relation. def merge_joins(scope, other) - joins_dependency, rest = other.joins_values.partition do |join| + association_joins, rest_joins = other.joins_values.partition do |join| case join when Hash, Symbol, Array true @@ -168,7 +169,7 @@ def merge_joins(scope, other) end end - join_dependency = ActiveRecord::Associations::JoinDependency.new(other.klass, joins_dependency, rest) + join_dependency = ActiveRecord::Associations::JoinDependency.new(other.klass, association_joins, rest_joins) scope.joins! join_dependency.join_constraints([], Arel::Nodes::InnerJoin).map(&:joins) end