diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-06-06 10:39:03 -0700 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-06-06 10:39:03 -0700 |
commit | e638c613d0ecfa44ddc3d328c0979318cf73e33e (patch) | |
tree | 36a2ba05339db71fba34f6992491835cf45cec8a /activerecord/lib/active_record | |
parent | b4fb80cbdab118125df0d48e74b6818fe5ec65d5 (diff) | |
parent | 4faaa811614b408b32422692ce36024442d02ffb (diff) | |
download | rails-e638c613d0ecfa44ddc3d328c0979318cf73e33e.tar.gz rails-e638c613d0ecfa44ddc3d328c0979318cf73e33e.tar.bz2 rails-e638c613d0ecfa44ddc3d328c0979318cf73e33e.zip |
Merge pull request #6629 from marcandre/destroy
Add ActiveRecord::Base#destroy!
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/errors.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 16 |
2 files changed, 20 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb index fc80f3081e..9b88bb8178 100644 --- a/activerecord/lib/active_record/errors.rb +++ b/activerecord/lib/active_record/errors.rb @@ -53,6 +53,10 @@ module ActiveRecord class RecordNotSaved < ActiveRecordError end + # Raised by ActiveRecord::Base.destroy! when a call to destroy would return false. + class RecordNotDestroyed < ActiveRecordError + end + # Raised when SQL statement cannot be executed by the database (for example, it's often the case for # MySQL when Ruby driver used is too old). class StatementInvalid < ActiveRecordError diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index a1bc39a32d..ec5670ba6e 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -122,6 +122,11 @@ module ActiveRecord # Deletes the record in the database and freezes this instance to reflect # that no changes should be made (since they can't be persisted). + # + # There's a series of callbacks associated with <tt>destroy</tt>. If + # the <tt>before_destroy</tt> callback return +false+ the action is cancelled + # and <tt>destroy</tt> returns +false+. See + # ActiveRecord::Callbacks for further details. def destroy raise ReadOnlyRecord if readonly? destroy_associations @@ -130,6 +135,17 @@ module ActiveRecord freeze end + # Deletes the record in the database and freezes this instance to reflect + # that no changes should be made (since they can't be persisted). + # + # There's a series of callbacks associated with <tt>destroy!</tt>. If + # the <tt>before_destroy</tt> callback return +false+ the action is cancelled + # and <tt>destroy!</tt> raises ActiveRecord::RecordNotDestroyed. See + # ActiveRecord::Callbacks for further details. + def destroy! + destroy || raise(ActiveRecord::RecordNotDestroyed) + end + # Returns an instance of the specified +klass+ with the attributes of the # current record. This is mostly useful in relation to single-table # inheritance structures where you want a subclass to appear as the |