From dd3ea17191e316aeebddaa7b176f6cfeee7a6365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 27 Mar 2014 16:10:17 -0500 Subject: [PATCH] Merge pull request #14469 from tiegz/timestamp_inheritance_fix Swap Timestamp/Callbacks order in ActiveRecord::Base --- activerecord/CHANGELOG.md | 4 ++++ activerecord/lib/active_record/base.rb | 2 +- activerecord/test/cases/timestamp_test.rb | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index b08f6ff38b36b..fe18ae995cf7e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Make possible to change `record_timestamps` inside Callbacks. + + *Tieg Zaharia* + * Fixed error where .persisted? throws SystemStackError for an unsaved model with a custom primary key that didn't save due to validation error. diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 9ec1feea97f15..1d47cba23438a 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -310,8 +310,8 @@ class Base include Locking::Optimistic include Locking::Pessimistic include AttributeMethods - include Callbacks include Timestamp + include Callbacks include Associations include ActiveModel::SecurePassword include AutosaveAssociation diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index 5308fa880849d..594b4fb07b434 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -71,6 +71,24 @@ def test_saving_when_instance_record_timestamps_is_false_doesnt_update_its_times assert_equal @previously_updated_at, @developer.updated_at end + def test_saving_when_callback_sets_record_timestamps_to_false_doesnt_update_its_timestamp + klass = Class.new(Developer) do + before_update :cancel_record_timestamps + def cancel_record_timestamps + self.record_timestamps = false + return true + end + end + + developer = klass.first + previously_updated_at = developer.updated_at + + developer.name = "New Name" + developer.save! + + assert_equal previously_updated_at, developer.updated_at + end + def test_touching_an_attribute_updates_timestamp previously_created_at = @developer.created_at @developer.touch(:created_at)