diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/validations.rb | 9 | ||||
-rwxr-xr-x | activerecord/test/validations_test.rb | 5 |
3 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 05c84bf578..0a798ef17b 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* 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] + * Added eager loading of associations as a way to solve the N+1 problem more gracefully without piggy-back queries. Example: for post in Post.find(:all, :limit => 100) 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. diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 736cad140a..a661193a98 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -64,6 +64,11 @@ class ValidationsTest < Test::Unit::TestCase assert_equal "is Wrong Update", r.errors.on("title"), "A reply with a bad content should contain an error" end + def test_invalid_record_exception + r = Reply.new + assert_raises(ActiveRecord::InvalidRecord) { r.save! } + end + def test_single_error_per_attr_iteration r = Reply.new r.save |