From 4faaa811614b408b32422692ce36024442d02ffb Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Tue, 5 Jun 2012 02:35:05 -0400 Subject: + ActiveRecord::Base#destroy! --- activerecord/lib/active_record/errors.rb | 4 ++++ activerecord/lib/active_record/persistence.rb | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) (limited to 'activerecord/lib/active_record') 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 destroy. If + # the before_destroy callback return +false+ the action is cancelled + # and destroy 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 destroy!. If + # the before_destroy callback return +false+ the action is cancelled + # and destroy! 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 -- cgit v1.2.3