public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/ddollar/rails.git
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
dhh (author)
Mon Apr 18 12:19:23 -0700 2005
commit  f46486d37ebe1d2d7354cf46dd024690a6d25c9a
tree    687cf35ae6e7804f125534e14c5379e6d1e9e389
parent  bd441bb06653c1696ccdce486add6e2d0b8e93d3
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* 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]
0
+
0
 * Added eager loading of associations as a way to solve the N+1 problem more gracefully without piggy-back queries. Example:
0
 
0
     for post in Post.find(:all, :limit => 100)
...
1
 
 
 
2
3
4
...
552
553
554
 
 
 
 
 
 
555
556
557
...
1
2
3
4
5
6
7
...
555
556
557
558
559
560
561
562
563
564
565
566
0
@@ -1,4 +1,7 @@
0
 module ActiveRecord
0
+ class InvalidRecord < ActiveRecordError #:nodoc:
0
+ end
0
+
0
   # Active Record validation is reported to and from this object, which is used by Base#save to
0
   # determine whether the object in a valid state to be saved. See usage example in Validations.
0
   class Errors
0
@@ -552,6 +555,12 @@ module ActiveRecord
0
       if perform_validation && valid? || !perform_validation then save_without_validation else false end
0
     end
0
 
0
+ # Attempts to save the record just like Base.save but will raise a InvalidRecord exception instead of returning false
0
+ # if the record is not valid.
0
+ def save!
0
+ valid? ? save_without_validation : raise(InvalidRecord)
0
+ end
0
+
0
     # Updates a single attribute and saves the record without going through the normal validation procedure.
0
     # This is especially useful for boolean flags on existing records. The regular +update_attribute+ method
0
     # in Base is replaced with this when the validations module is mixed in, which it is by default.
...
64
65
66
 
 
 
 
 
67
68
69
...
64
65
66
67
68
69
70
71
72
73
74
0
@@ -64,6 +64,11 @@ class ValidationsTest < Test::Unit::TestCase
0
     assert_equal "is Wrong Update", r.errors.on("title"), "A reply with a bad content should contain an error"
0
   end
0
 
0
+ def test_invalid_record_exception
0
+ r = Reply.new
0
+ assert_raises(ActiveRecord::InvalidRecord) { r.save! }
0
+ end
0
+
0
   def test_single_error_per_attr_iteration
0
     r = Reply.new
0
     r.save

Comments

    No one has commented yet.