diff options
author | Recursive Madman <recursive.madman@gmx.de> | 2014-11-19 12:34:25 +0100 |
---|---|---|
committer | Recursive Madman <recursive.madman@gmx.de> | 2014-11-26 14:41:49 +0100 |
commit | 3e30c5d4222f816cdffd5241d56ef9442f6d1903 (patch) | |
tree | a86bf99ed5f5fa12f127d17588bf3ffa92116de2 /activerecord/lib | |
parent | 644696e2ac04099623a0425016470cc5485e067b (diff) | |
download | rails-3e30c5d4222f816cdffd5241d56ef9442f6d1903.tar.gz rails-3e30c5d4222f816cdffd5241d56ef9442f6d1903.tar.bz2 rails-3e30c5d4222f816cdffd5241d56ef9442f6d1903.zip |
Add #record attribute to RecordNotFound and RecordDestroyed exceptions.
This allows these exceptions to be handled generically in conjunction with RecordInvalid.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/errors.rb | 19 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 4 |
2 files changed, 21 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb index 5b3fdf16f5..ca4fede7a2 100644 --- a/activerecord/lib/active_record/errors.rb +++ b/activerecord/lib/active_record/errors.rb @@ -52,10 +52,29 @@ module ActiveRecord # Raised by ActiveRecord::Base.save! and ActiveRecord::Base.create! methods when record cannot be # saved because record is invalid. class RecordNotSaved < ActiveRecordError + attr_reader :record + + def initialize(record) + @record = record + super() + end end # Raised by ActiveRecord::Base.destroy! when a call to destroy would return false. + # + # begin + # complex_operation_that_internally_calls_destroy! + # rescue ActiveRecord::RecordNotDestroyed => invalid + # puts invalid.record.errors + # end + # class RecordNotDestroyed < ActiveRecordError + attr_reader :record + + def initialize(record) + @record = record + super() + end end # Superclass for all database execution errors. diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index ee3b7b6163..8d84a3acae 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -139,7 +139,7 @@ module ActiveRecord # Attributes marked as readonly are silently ignored if the record is # being updated. def save!(*) - create_or_update || raise(RecordNotSaved) + create_or_update || raise(RecordNotSaved, self) end # Deletes the record in the database and freezes this instance to @@ -181,7 +181,7 @@ module ActiveRecord # and <tt>destroy!</tt> raises ActiveRecord::RecordNotDestroyed. See # ActiveRecord::Callbacks for further details. def destroy! - destroy || raise(ActiveRecord::RecordNotDestroyed) + destroy || raise(ActiveRecord::RecordNotDestroyed, self) end # Returns an instance of the specified +klass+ with the attributes of the |