aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-09-11 14:48:40 +0100
committerJon Leighton <j@jonathanleighton.com>2011-09-13 00:01:58 +0100
commiteecfa84a9065da2b1bd1e02f37e8653a2825c624 (patch)
tree103ae42eb5c8f6206a5eac7b731307b3348dadb7 /activerecord/lib/active_record/attribute_methods.rb
parent50d395f96ea05da1e02459688e94bff5872c307b (diff)
downloadrails-eecfa84a9065da2b1bd1e02f37e8653a2825c624.tar.gz
rails-eecfa84a9065da2b1bd1e02f37e8653a2825c624.tar.bz2
rails-eecfa84a9065da2b1bd1e02f37e8653a2825c624.zip
Always generate attribute methods on the base class.
This fixes a situation I encountered where a subclass would cache the name of a generated attribute method in @_defined_class_methods. Then, when the superclass has it's attribute methods undefined, the subclass would always have to dispatch through method_missing, because the presence of the attribute in @_defined_class_methods would mean that it is never generated again, even if undefine_attribute_methods is called on the subclass. There various other confusing edge cases like this. STI classes share columns, so let's just keep all the attribute method generation state isolated to the base class.
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods.rb')
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb23
1 files changed, 18 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index 1937ac4272..a8cb89fb65 100644
--- a/activerecord/lib/active_record/attribute_methods.rb
+++ b/activerecord/lib/active_record/attribute_methods.rb
@@ -11,17 +11,30 @@ module ActiveRecord
# accessors, mutators and query methods.
def define_attribute_methods
return if attribute_methods_generated?
- super(column_names)
- @attribute_methods_generated = true
+
+ if base_class == self
+ super(column_names)
+ @attribute_methods_generated = true
+ else
+ base_class.define_attribute_methods
+ end
end
def attribute_methods_generated?
- @attribute_methods_generated ||= false
+ if base_class == self
+ @attribute_methods_generated ||= false
+ else
+ base_class.attribute_methods_generated?
+ end
end
def undefine_attribute_methods(*args)
- super
- @attribute_methods_generated = false
+ if base_class == self
+ super
+ @attribute_methods_generated = false
+ else
+ base_class.undefine_attribute_methods(*args)
+ end
end
# Checks whether the method is defined in the model or any of its subclasses