diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb index 9631a7d242ce3..577d5cbf4522c 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -50,7 +50,7 @@ def class_attribute(*attrs) singleton_class.send(:define_method, attr) { value } end - define_method(attr) { self.class.send(attr) } + define_method(attr) { self.singleton_class.send(attr) } define_method(:"#{attr}?") { !!send(attr) } define_method(:"#{attr}=") do |value| singleton_class.remove_possible_method(attr) diff --git a/activesupport/test/core_ext/class/attribute_test.rb b/activesupport/test/core_ext/class/attribute_test.rb index 06b4cf075f7f4..24aa5c0ebad77 100644 --- a/activesupport/test/core_ext/class/attribute_test.rb +++ b/activesupport/test/core_ext/class/attribute_test.rb @@ -59,4 +59,10 @@ def setup object = Class.new { class_attribute :setting, :instance_writer => false }.new assert_raise(NoMethodError) { object.setting = 'boom' } end + + test 'works well with singleton classes' do + object = @klass.new + object.singleton_class.setting = 'foo' + assert_equal 'foo', object.setting + end end