aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/validations.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-04-18 19:19:23 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-04-18 19:19:23 +0000
commitf46486d37ebe1d2d7354cf46dd024690a6d25c9a (patch)
tree687cf35ae6e7804f125534e14c5379e6d1e9e389 /activerecord/lib/active_record/validations.rb
parentbd441bb06653c1696ccdce486add6e2d0b8e93d3 (diff)
downloadrails-f46486d37ebe1d2d7354cf46dd024690a6d25c9a.tar.gz
rails-f46486d37ebe1d2d7354cf46dd024690a6d25c9a.tar.bz2
rails-f46486d37ebe1d2d7354cf46dd024690a6d25c9a.zip
Added Base.save! that attempts to save the record just like Base.save but will raise a InvalidRecord exception instead of returning false if the record is not valid [After much pestering from Dave Thomas]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1215 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/validations.rb')
-rwxr-xr-xactiverecord/lib/active_record/validations.rb9
1 files changed, 9 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index af92ac6db8..6930a2a9d5 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -1,4 +1,7 @@
module ActiveRecord
+ class InvalidRecord < ActiveRecordError #:nodoc:
+ end
+
# Active Record validation is reported to and from this object, which is used by Base#save to
# determine whether the object in a valid state to be saved. See usage example in Validations.
class Errors
@@ -552,6 +555,12 @@ module ActiveRecord
if perform_validation && valid? || !perform_validation then save_without_validation else false end
end
+ # Attempts to save the record just like Base.save but will raise a InvalidRecord exception instead of returning false
+ # if the record is not valid.
+ def save!
+ valid? ? save_without_validation : raise(InvalidRecord)
+ end
+
# Updates a single attribute and saves the record without going through the normal validation procedure.
# This is especially useful for boolean flags on existing records. The regular +update_attribute+ method
# in Base is replaced with this when the validations module is mixed in, which it is by default.