diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-05-20 10:33:50 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-05-20 10:37:17 +0200 |
commit | 60bd2432b2f1fa5a84554aea38b8971368b79cfe (patch) | |
tree | d5868041e7ccb1c12141b89cc9b0cfd5a246435a | |
parent | 7591e8c1a752c92091225c3b8503ea31453af595 (diff) | |
download | rails-60bd2432b2f1fa5a84554aea38b8971368b79cfe.tar.gz rails-60bd2432b2f1fa5a84554aea38b8971368b79cfe.tar.bz2 rails-60bd2432b2f1fa5a84554aea38b8971368b79cfe.zip |
docs, `instantiate` expects `String` keys. [Rafal Piekarski & Yves Senn]
Closes #15122
Closes #15107
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/persistence_test.rb | 11 |
2 files changed, 15 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 13d7432773..b74e340b3e 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -37,7 +37,7 @@ module ActiveRecord end # Given an attributes hash, +instantiate+ returns a new instance of - # the appropriate class. + # the appropriate class. Accepts only keys as strings. # # For example, +Post.all+ may return Comments, Messages, and Emails # by storing the record's subclass in a +type+ attribute. By calling @@ -46,10 +46,10 @@ module ActiveRecord # # See +ActiveRecord::Inheritance#discriminate_class_for_record+ to see # how this "single-table" inheritance mapping is implemented. - def instantiate(record, column_types = {}) - klass = discriminate_class_for_record(record) + def instantiate(attributes, column_types = {}) + klass = discriminate_class_for_record(attributes) column_types = klass.decorate_columns(column_types.dup) - klass.allocate.init_with('attributes' => record, 'column_types' => column_types) + klass.allocate.init_with('attributes' => attributes, 'column_types' => column_types) end private diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index 5d963098fb..bc5ccd0fe9 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -843,4 +843,15 @@ class PersistenceTest < ActiveRecord::TestCase assert_equal "Wright Glider", Aircraft.last.name end + + def test_instantiate_creates_a_new_instance + post = Post.instantiate("title" => "appropriate documentation", "type" => "SpecialPost") + assert_equal "appropriate documentation", post.title + assert_instance_of SpecialPost, post + + # body was not initialized + assert_raises ActiveModel::MissingAttributeError do + post.body + end + end end |