aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPrem Sichanugrist <s@sikac.hu>2017-02-21 19:59:56 +0900
committerPrem Sichanugrist <s@sikac.hu>2017-02-21 20:33:32 +0900
commit0b157f8ded03ef988308a135e12c9bc99a1cc703 (patch)
tree62b97831d0f6120526acdc63fe4df78b21744030 /activerecord
parentc1b64429b1af45a4a526b3b5bb5a306a0d51e28a (diff)
downloadrails-0b157f8ded03ef988308a135e12c9bc99a1cc703.tar.gz
rails-0b157f8ded03ef988308a135e12c9bc99a1cc703.tar.bz2
rails-0b157f8ded03ef988308a135e12c9bc99a1cc703.zip
Fix `define_attribute_method` with Symbol in AR
This issue is only appear when you try to call `define_attribute_method` and passing a symbol in Active Record. It does not appear in isolation in Active Model itself. Before this patch, when you run `User.define_attribute_method :foo`, you will get: NoMethodError: undefined method `unpack' for :foo:Symbol from activerecord/lib/active_record/attribute_methods/read.rb:28:in `define_method_attribute' from activerecord/lib/active_record/attribute_methods/primary_key.rb:61:in `define_method_attribute' from activemodel/lib/active_model/attribute_methods.rb:292:in `block in define_attribute_method' from activemodel/lib/active_model/attribute_methods.rb:285:in `each' from activemodel/lib/active_model/attribute_methods.rb:285:in `define_attribute_method' This patch contains both a fix in Active Model and a test in Active Record for this error.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb7
1 files changed, 7 insertions, 0 deletions
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index 3dc0c0ce53..4d24a980dc 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -866,6 +866,13 @@ class AttributeMethodsTest < ActiveRecord::TestCase
assert subklass.method_defined?(:id), "subklass is missing id method"
end
+ test "define_attribute_method works with both symbol and string" do
+ klass = Class.new(ActiveRecord::Base)
+
+ assert_nothing_raised { klass.define_attribute_method(:foo) }
+ assert_nothing_raised { klass.define_attribute_method("bar") }
+ end
+
test "read_attribute with nil should not asplode" do
assert_nil Topic.new.read_attribute(nil)
end