Skip to content

Commit 18dddca

Browse files
Improve documentation of RecordNotSaved and RecordNotDestroyed [ci-skip]
Add examples and explain that RecordNotDestroyed triggered by throwing :abort in callbacks. Co-authored-by: Carlos Antonio da Silva <[email protected]>
1 parent 03187f6 commit 18dddca

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

activerecord/lib/active_record/errors.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,17 @@ def initialize(message = nil, model = nil, primary_key = nil, id = nil)
130130

131131
# Raised by {ActiveRecord::Base#save!}[rdoc-ref:Persistence#save!] and
132132
# {ActiveRecord::Base.update_attribute!}[rdoc-ref:Persistence#update_attribute!]
133-
# methods when a record is failed to validate or cannot be saved due to any of the
133+
# methods when a record failed to validate or cannot be saved due to any of the
134134
# <tt>before_*</tt> callbacks throwing +:abort+. See
135-
# ActiveRecord::Callbacks for further details
135+
# ActiveRecord::Callbacks for further details.
136+
#
137+
# class Product < ActiveRecord::Base
138+
# before_save do
139+
# throw :abort if price < 0
140+
# end
141+
# end
142+
#
143+
# Product.create! # => raises an ActiveRecord::RecordNotSaved
136144
class RecordNotSaved < ActiveRecordError
137145
attr_reader :record
138146

@@ -143,15 +151,17 @@ def initialize(message = nil, record = nil)
143151
end
144152

145153
# Raised by {ActiveRecord::Base#destroy!}[rdoc-ref:Persistence#destroy!]
146-
# when a call to {#destroy}[rdoc-ref:Persistence#destroy]
147-
# would return false.
154+
# when a record cannot be destroyed due to any of the
155+
# <tt>before_destroy</tt> callbacks throwing +:abort+. See
156+
# ActiveRecord::Callbacks for further details.
148157
#
149-
# begin
150-
# complex_operation_that_internally_calls_destroy!
151-
# rescue ActiveRecord::RecordNotDestroyed => invalid
152-
# puts invalid.record.errors
158+
# class User < ActiveRecord::Base
159+
# before_destroy do
160+
# throw :abort if still_active?
161+
# end
153162
# end
154163
#
164+
# User.first.destroy! # => raises an ActiveRecord::RecordNotDestroyed
155165
class RecordNotDestroyed < ActiveRecordError
156166
attr_reader :record
157167

0 commit comments

Comments
 (0)