aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record')
-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.