From 5f5c38d9dd15c3bbbf7f21cf61d460c4cdf48ffa Mon Sep 17 00:00:00 2001 From: ender672 Date: Tue, 9 Feb 2010 18:00:24 -0800 Subject: [PATCH] Don't re-scope with current_scoped_methods_when_defined in Scope#method_missing The test to see if re-scoping is needed in Scope#method_missing was calling Scope#== which loads the scope. This can cause extra and unneeded db queries. All tests pass fine without re-scoping. --- activerecord/lib/active_record/named_scope.rb | 9 +-------- activerecord/test/cases/named_scope_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index ff6c041..5b2ec11 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -126,8 +126,6 @@ module ActiveRecord end class Scope < Relation - attr_accessor :current_scoped_methods_when_defined - delegate :scopes, :with_scope, :with_exclusive_scope, :scoped_methods, :scoped, :to => :klass def self.init(klass, options, &block) @@ -144,7 +142,6 @@ module ActiveRecord Array.wrap(options[:extend]).each {|extension| relation.send(:extend, extension) } if options.is_a?(Hash) relation.send(:extend, Module.new(&block)) if block_given? - relation.current_scoped_methods_when_defined = klass.send(:current_scoped_methods) relation end @@ -173,11 +170,7 @@ module ActiveRecord def method_missing(method, *args, &block) if klass.respond_to?(method) with_scope(self) do - if current_scoped_methods_when_defined && !scoped_methods.include?(current_scoped_methods_when_defined) && !scopes.include?(method) - with_scope(current_scoped_methods_when_defined) { klass.send(method, *args, &block) } - else - klass.send(method, *args, &block) - end + klass.send(method, *args, &block) end else super diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 894d963..8c4da9c 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -217,6 +217,14 @@ class NamedScopeTest < ActiveRecord::TestCase end end + def test_chaining_should_not_load_results + topics = Topic.base + + assert_queries(0) do + topics.approved.rejected.approved + end + end + def test_any_should_call_proxy_found_if_using_a_block topics = Topic.base assert_queries(1) do -- 1.6.3.3