From ac156060a4dd81ea1f7730affb1491ceb3c973d0 Mon Sep 17 00:00:00 2001 From: Ben Standefer Date: Wed, 1 Jun 2016 12:03:30 -0700 Subject: Improve error message when record touch fails. The current error message only indicates that a touch can fail because the record is new. In practice, we saw cases where touches were failing because the record had been destroyed. `persisted?` checks `new_record?` *and* `destroyed?`. It was confusing to get a message about a new record when in reality we were destroying records. I also included a helpful tip for users to consider using `persisted?`, `new_record?`, or `destroyed?` before touching. --- activerecord/lib/active_record/persistence.rb | 7 ++++++- activerecord/lib/active_record/touch_later.rb | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index afed5e5e85..ebdb9efba2 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -479,7 +479,12 @@ module ActiveRecord # ball.touch(:updated_at) # => raises ActiveRecordError # def touch(*names, time: nil) - raise ActiveRecordError, "cannot touch on a new record object" unless persisted? + unless persisted? + raise ActiveRecordError, <<-end_error.strip_heredoc + cannot touch on a new or destroyed record object. Consider using + persisted?, new_record?, or destroyed? before touching + end_error + end time ||= current_time_from_proper_timezone attributes = timestamp_attributes_for_update_in_model diff --git a/activerecord/lib/active_record/touch_later.rb b/activerecord/lib/active_record/touch_later.rb index 9a80a63e28..5c41ab6e39 100644 --- a/activerecord/lib/active_record/touch_later.rb +++ b/activerecord/lib/active_record/touch_later.rb @@ -8,7 +8,12 @@ module ActiveRecord end def touch_later(*names) # :nodoc: - raise ActiveRecordError, "cannot touch on a new record object" unless persisted? + unless persisted? + raise ActiveRecordError, <<-end_error.strip_heredoc + cannot touch on a new or destroyed record object. Consider using + persisted?, new_record?, or destroyed? before touching + end_error + end @_defer_touch_attrs ||= timestamp_attributes_for_update_in_model @_defer_touch_attrs |= names -- cgit v1.2.3