Skip to content

Commit b45b998

Browse files
committed
Merge pull request rails#16456 from lsylvester/improve-active-record-error-message-on-update_columns-for-destroyed-records
Change error message in update_columns for destroyed objects
2 parents 49798b5 + 0f99aa6 commit b45b998

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Diff for: activerecord/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* When calling `update_columns` on a record that is not persisted, the error
2+
message now reflects whether that object is a new record or has been
3+
destroyed.
4+
5+
*Lachlan Sylvester*
6+
17
* Define `id_was` to get the previous value of the primary key.
28

39
Currently when we call id_was and we have a custom primary key name

Diff for: activerecord/lib/active_record/persistence.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ def update_column(name, value)
287287
# This method raises an +ActiveRecord::ActiveRecordError+ when called on new
288288
# objects, or when at least one of the attributes is marked as readonly.
289289
def update_columns(attributes)
290-
raise ActiveRecordError, "cannot update on a new record object" unless persisted?
290+
raise ActiveRecordError, "cannot update a new record" if new_record?
291+
raise ActiveRecordError, "cannot update a destroyed record" if destroyed?
291292

292293
attributes.each_key do |key|
293294
verify_readonly_attribute(key.to_s)

0 commit comments

Comments
 (0)