From cd26b6ae7c1546ef8f38302661bbedf8cb487311 Mon Sep 17 00:00:00 2001 From: Neeraj Singh and John Leighton Date: Thu, 7 Mar 2013 18:35:02 -0500 Subject: Combine scope conditions using AND Currently Post.active.inactive will result in Post.inactive since the last where clause wins when scopes are merged. This pull request will merge all scopes ( barring defaul scope) using AND. The default scope will be overridden if another scope acts on the same where clause. closes #7365 --- activerecord/lib/active_record/scoping/named.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/scoping/named.rb') diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 01fbb96b8e..708b27dd7e 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -159,10 +159,20 @@ module ActiveRecord end singleton_class.send(:define_method, name) do |*args| - options = body.respond_to?(:call) ? unscoped { body.call(*args) } : body - relation = all.merge(options) + if body.respond_to?(:call) + scope = extension ? body.call(*args).extending(extension) : body.call(*args) - extension ? relation.extending(extension) : relation + if scope + default_scoped = scope.default_scoped + scope = relation.merge(scope) + scope.default_scoped = default_scoped + end + + else + scope = body + end + + scope || all end end end -- cgit v1.2.3