diff options
author | José Valim <jose.valim@gmail.com> | 2011-05-10 10:30:27 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-05-10 10:30:27 +0200 |
commit | 9e4b715d790aa84dfb3d7aa332e0012cbc264394 (patch) | |
tree | cb6852861de2744552a3ea2a09c7e83344b37ca0 | |
parent | 86a0f7f73594e5ba56b78b74c16cd6efa7e6cfa9 (diff) | |
download | rails-9e4b715d790aa84dfb3d7aa332e0012cbc264394.tar.gz rails-9e4b715d790aa84dfb3d7aa332e0012cbc264394.tar.bz2 rails-9e4b715d790aa84dfb3d7aa332e0012cbc264394.zip |
Ensure assign_attributes and update_attributes do not fail on nil, closes #478.
-rw-r--r-- | activerecord/lib/active_record/base.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/mass_assignment_security_test.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/persistence_test.rb | 4 |
3 files changed, 10 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 67af21c9a0..d07f9365b3 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1688,6 +1688,8 @@ MSG # user.name # => "Josh" # user.is_admin? # => true def assign_attributes(new_attributes, options = {}) + return unless new_attributes + attributes = new_attributes.stringify_keys role = options[:as] || :default diff --git a/activerecord/test/cases/mass_assignment_security_test.rb b/activerecord/test/cases/mass_assignment_security_test.rb index c81015b7c2..062a642e50 100644 --- a/activerecord/test/cases/mass_assignment_security_test.rb +++ b/activerecord/test/cases/mass_assignment_security_test.rb @@ -87,6 +87,10 @@ class MassAssignmentSecurityTest < ActiveRecord::TestCase end end + def test_mass_assigning_does_not_choke_on_nil + Firm.new.assign_attributes(nil) + end + def test_assign_attributes_uses_default_role_when_no_role_is_provided p = LoosePerson.new p.assign_attributes(attributes_hash) diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index b066575af8..57d1441128 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -336,6 +336,10 @@ class PersistencesTest < ActiveRecord::TestCase assert !Topic.find(1).approved? end + def test_update_attribute_does_not_choke_on_nil + assert Topic.find(1).update_attributes(nil) + end + def test_update_attribute_for_readonly_attribute minivan = Minivan.find('m1') assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') } |