diff options
author | Godfrey Chan <godfreykfc@gmail.com> | 2014-06-14 02:18:36 -0700 |
---|---|---|
committer | Godfrey Chan <godfreykfc@gmail.com> | 2014-06-14 02:29:38 -0700 |
commit | dd1ec62578db6f7a03d1c07546b00a008a0c629d (patch) | |
tree | 6323bdb14bcaa8a17674815f7a31af28689399f9 /activerecord | |
parent | ee4e86fa4bb6ab0406127e2708e9c3db346ba314 (diff) | |
download | rails-dd1ec62578db6f7a03d1c07546b00a008a0c629d.tar.gz rails-dd1ec62578db6f7a03d1c07546b00a008a0c629d.tar.bz2 rails-dd1ec62578db6f7a03d1c07546b00a008a0c629d.zip |
Fixed a regression introduced in 84cf156
84cf156 (PR #15694) introduced a subtle regression. There are actually three
distinct entry points to creating an AR object – via .new (i.e. #initialize),
via #init_with (e.g. from YAML or database queries) and via .allocate.
With the patch in 84cf156, attribute methods and respond_to? will not work
correctly when objects are allocated directly, without going through either
The reason this test case didn't catch the regression was that the `Topic`
class is shared between test cases, so by the time this test case is ran the
attribute methods are very likely to be defined.
Switching to use a fresh anonymous class in the test to ensure we surface this
problem in the future.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/core.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/attribute_methods_test.rb | 6 |
2 files changed, 10 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index d39e5fddfe..47183752e7 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -108,6 +108,11 @@ module ActiveRecord end module ClassMethods + def allocate + define_attribute_methods + super + end + def initialize_find_by_cache self.find_by_statement_cache = {}.extend(Mutex_m) end diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index f832ca3451..f8e2609572 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -143,7 +143,11 @@ class AttributeMethodsTest < ActiveRecord::TestCase # Syck calls respond_to? before actually calling initialize def test_respond_to_with_allocated_object - topic = Topic.allocate + klass = Class.new(ActiveRecord::Base) do + self.table_name = 'topics' + end + + topic = klass.allocate assert !topic.respond_to?("nothingness") assert !topic.respond_to?(:nothingness) assert_respond_to topic, "title" |