Skip to content

Commit dd3ea17

Browse files
committed
Merge pull request rails#14469 from tiegz/timestamp_inheritance_fix
Swap Timestamp/Callbacks order in ActiveRecord::Base
1 parent 5cf456a commit dd3ea17

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Make possible to change `record_timestamps` inside Callbacks.
2+
3+
*Tieg Zaharia*
4+
15
* Fixed error where .persisted? throws SystemStackError for an unsaved model with a
26
custom primary key that didn't save due to validation error.
37

activerecord/lib/active_record/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ class Base
310310
include Locking::Optimistic
311311
include Locking::Pessimistic
312312
include AttributeMethods
313-
include Callbacks
314313
include Timestamp
314+
include Callbacks
315315
include Associations
316316
include ActiveModel::SecurePassword
317317
include AutosaveAssociation

activerecord/test/cases/timestamp_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ def test_saving_when_instance_record_timestamps_is_false_doesnt_update_its_times
7171
assert_equal @previously_updated_at, @developer.updated_at
7272
end
7373

74+
def test_saving_when_callback_sets_record_timestamps_to_false_doesnt_update_its_timestamp
75+
klass = Class.new(Developer) do
76+
before_update :cancel_record_timestamps
77+
def cancel_record_timestamps
78+
self.record_timestamps = false
79+
return true
80+
end
81+
end
82+
83+
developer = klass.first
84+
previously_updated_at = developer.updated_at
85+
86+
developer.name = "New Name"
87+
developer.save!
88+
89+
assert_equal previously_updated_at, developer.updated_at
90+
end
91+
7492
def test_touching_an_attribute_updates_timestamp
7593
previously_created_at = @developer.created_at
7694
@developer.touch(:created_at)

0 commit comments

Comments
 (0)