From ef4fb3dee2e897b245ee6f545232378db3de7b38 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 2 Oct 2006 03:21:32 +0000 Subject: save! shouldn't validate twice. Closes #6324. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5215 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 4 ++-- activerecord/test/validations_test.rb | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 7272741329..7107d825a3 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* save! shouldn't validate twice. #6324 [maiha, Bob Silva] + * Association collections have an _ids reader method to match the existing writer for collection_select convenience (e.g. employee.task_ids). The writer method skips blank ids so you can safely do @employee.task_ids = params[:tasks] without checking every time for an empty list or blank values. #1887, #5780 [Michael Schuerig] * Add an attribute reader method for ActiveRecord::Base.observers [Rick Olson] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 43f631ef6c..af2645eeb7 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1527,14 +1527,13 @@ module ActiveRecord #:nodoc: # * No record exists: Creates a new record with values matching those of the object attributes. # * A record does exist: Updates the record with values matching those of the object attributes. def save - raise ReadOnlyRecord if readonly? create_or_update end # Attempts to save the record, but instead of just returning false if it couldn't happen, it raises a # RecordNotSaved exception def save! - save || raise(RecordNotSaved) + create_or_update || raise(RecordNotSaved) end # Deletes the record in the database and freezes this instance to reflect that no changes should @@ -1765,6 +1764,7 @@ module ActiveRecord #:nodoc: private def create_or_update + raise ReadOnlyRecord if readonly? if new_record? then create else update end true end diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 936c36e6cb..d0d644a567 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -141,6 +141,12 @@ class ValidationsTest < Test::Unit::TestCase assert reply.save(false) end + def test_create_without_validation_bang + count = Reply.count + assert_nothing_raised { Reply.new.save_without_validation! } + assert count+1, Reply.count + end + def test_validates_each perform = true hits = 0 -- cgit v1.2.3