Permalink
Browse files

Added update_attributes! which uses save! to raise an exception if a …

…validation error prevents saving (closes #6192) [jonathan]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5256 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9fd88d7 commit 55aab5b8acb72967228dc38f6432e0f126a94d77 @dhh dhh committed Oct 9, 2006
Showing with 46 additions and 1 deletion.
  1. +2 −0 activerecord/CHANGELOG
  2. +6 −0 activerecord/lib/active_record/base.rb
  3. +38 −1 activerecord/test/base_test.rb
View
2 activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added update_attributes! which uses save! to raise an exception if a validation error prevents saving #6192 [jonathan]
+
* Deprecated add_on_boundary_breaking (use validates_length_of instead) #6292 [BobSilva]
* The has_many create method works with polymorphic associations. #6361 [Dan Peterson]
View
6 activerecord/lib/active_record/base.rb
@@ -1576,6 +1576,12 @@ def update_attributes(attributes)
self.attributes = attributes
save
end
+
+ # Updates an object just like Base.update_attributes but calls save! instead of save so an exception is raised if the record is invalid.
+ def update_attributes!(attributes)
+ self.attributes = attributes
+ save!
+ end
# Initializes the +attribute+ to zero if nil and adds one. Only makes sense for number-based attributes. Returns self.
def increment(attribute)
View
39 activerecord/test/base_test.rb
@@ -149,8 +149,11 @@ def test_create
def test_save!
topic = Topic.new(:title => "New Topic")
assert topic.save!
- end
+ reply = Reply.new
+ assert_raise(ActiveRecord::RecordInvalid) { reply.save! }
+ end
+
def test_hashes_not_mangled
new_topic = { :title => "New Topic" }
new_topic_values = { :title => "AnotherTopic" }
@@ -646,6 +649,40 @@ def test_update_attribute
assert !Topic.find(1).approved?
end
+ def test_update_attributes
+ topic = Topic.find(1)
+ assert !topic.approved?
+ assert_equal "The First Topic", topic.title
+
+ topic.update_attributes("approved" => true, "title" => "The First Topic Updated")
+ topic.reload
+ assert topic.approved?
+ assert_equal "The First Topic Updated", topic.title
+
+ topic.update_attributes(:approved => false, :title => "The First Topic")
+ topic.reload
+ assert !topic.approved?
+ assert_equal "The First Topic", topic.title
+ end
+
+ def test_update_attributes!
+ reply = Reply.find(2)
+ assert_equal "The Second Topic's of the day", reply.title
+ assert_equal "Have a nice day", reply.content
+
+ reply.update_attributes!("title" => "The Second Topic's of the day updated", "content" => "Have a nice evening")
+ reply.reload
+ assert_equal "The Second Topic's of the day updated", reply.title
+ assert_equal "Have a nice evening", reply.content
+
+ reply.update_attributes!(:title => "The Second Topic's of the day", :content => "Have a nice day")
+ reply.reload
+ assert_equal "The Second Topic's of the day", reply.title
+ assert_equal "Have a nice day", reply.content
+
+ assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") }
+ end
+
def test_mass_assignment_protection
firm = Firm.new
firm.attributes = { "name" => "Next Angle", "rating" => 5 }

0 comments on commit 55aab5b

Please sign in to comment.