From 98e69078d5e2fe9a13bd912bbb5da00be4d43497 Mon Sep 17 00:00:00 2001 From: Jan Habermann Date: Thu, 3 Apr 2014 01:52:42 +0200 Subject: Properly handle scoping with has_many :through. Fixes #14537. --- .../lib/active_record/associations/through_association.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index ba7d2a3782..66b1616949 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -14,9 +14,14 @@ module ActiveRecord def target_scope scope = super chain.drop(1).each do |reflection| + relation = if reflection.scope + reflection.klass.all.instance_eval(&reflection.scope) + else + reflection.klass.all + end + scope.merge!( - reflection.klass.all. - except(:select, :create_with, :includes, :preload, :joins, :eager_load) + relation.except(:select, :create_with, :includes, :preload, :joins, :eager_load) ) end scope -- cgit v1.2.3 From 384984d7ca188d1dae59b204650b8319de9f9f05 Mon Sep 17 00:00:00 2001 From: Jan Habermann Date: Thu, 3 Apr 2014 12:40:38 +0200 Subject: Simplify the code in target_scope --- activerecord/lib/active_record/associations/through_association.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index 66b1616949..942abf7df1 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -14,11 +14,8 @@ module ActiveRecord def target_scope scope = super chain.drop(1).each do |reflection| - relation = if reflection.scope - reflection.klass.all.instance_eval(&reflection.scope) - else - reflection.klass.all - end + relation = reflection.klass.all + relation.instance_eval(&reflection.scope) if reflection.scope scope.merge!( relation.except(:select, :create_with, :includes, :preload, :joins, :eager_load) -- cgit v1.2.3 From 47a04b8bbf35238639b00bfab500a84607d8d871 Mon Sep 17 00:00:00 2001 From: Jan Habermann Date: Thu, 3 Apr 2014 19:03:31 +0200 Subject: Minor improvement: Use the merge method on the relation instead of instance_eval directly. --- activerecord/lib/active_record/associations/through_association.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index 942abf7df1..ce65948133 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -15,7 +15,7 @@ module ActiveRecord scope = super chain.drop(1).each do |reflection| relation = reflection.klass.all - relation.instance_eval(&reflection.scope) if reflection.scope + relation.merge!(reflection.scope) if reflection.scope scope.merge!( relation.except(:select, :create_with, :includes, :preload, :joins, :eager_load) -- cgit v1.2.3