diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 0f421560f087430894f4902dc0f1a6deabf7db82..d29163226024f90e7e69b750edc044a4463fc044 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -102,10 +102,9 @@ def scoped(options = nil) def scope(name, scope_options = {}) name = name.to_sym valid_scope_name?(name) - extension = Module.new(&Proc.new) if block_given? - scopes[name] = lambda do |*args| + scope_proc = lambda do |*args| options = scope_options.respond_to?(:call) ? scope_options.call(*args) : scope_options relation = if options.is_a?(Hash) @@ -119,6 +118,8 @@ def scope(name, scope_options = {}) extension ? relation.extending(extension) : relation end + self.scopes = self.scopes.merge name => scope_proc + singleton_class.send(:redefine_method, name, &scopes[name]) end diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index d05b0ff9471bf355eaf68ebf9150ba9d5d360605..fb050c3e52698908c7ac5c9bfcce692bcbcea8e4 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -64,6 +64,10 @@ def test_subclasses_inherit_scopes assert Reply.scopes.include?(:base) assert_equal Reply.find(:all), Reply.base end + + def test_classes_dont_inherit_subclasses_scopes + assert !ActiveRecord::Base.scopes.include?(:base) + end def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specified assert !Topic.find(:all, :conditions => {:approved => true}).empty?