Skip to content
Browse files

+ Add test for auto timestamps update of both old & new parent records

  • Loading branch information...
1 parent 36f7732 commit 7fc339059cbace0af8f8dbfb5c68db3f5dffd5a3 @PikachuEXE PikachuEXE committed with adamgamble
Showing with 38 additions and 0 deletions.
  1. +34 −0 activerecord/test/cases/timestamp_test.rb
  2. +4 −0 activerecord/test/fixtures/toys.yml
View
34 activerecord/test/cases/timestamp_test.rb
@@ -176,6 +176,40 @@ def self.name; 'Toy'; end
assert_not_equal time, owner.updated_at
end
+ def test_changing_parent_of_a_record_touches_both_new_and_old_parent_record_and_grandparent_record
+ klass = Class.new(ActiveRecord::Base) do
+ def self.name; 'Toy'; end
+ belongs_to :pet, touch: true
+ end
+
+ toy1 = klass.find(1)
+ old_pet = toy1.pet
+ old_owner = old_pet.owner
+
+ toy2 = klass.find(2)
+ new_pet = toy2.pet
+ new_owner = new_pet.owner
+ time = 3.days.ago
+
+ old_pet.update_columns(updated_at: time)
+ old_owner.update_columns(updated_at: time)
+ new_pet.update_columns(updated_at: time)
+ new_owner.update_columns(updated_at: time)
+
+ toy1.pet = new_pet
+ toy1.save!
+
+ old_pet.reload
+ old_owner.reload
+ new_pet.reload
+ new_owner.reload
+
+ assert_not_equal time, old_pet.updated_at
+ assert_not_equal time, old_owner.updated_at
+ assert_not_equal time, new_pet.updated_at
+ assert_not_equal time, new_owner.updated_at
+ end
+
def test_timestamp_attributes_for_create
toy = Toy.first
assert_equal toy.send(:timestamp_attributes_for_create), [:created_at, :created_on]
View
4 activerecord/test/fixtures/toys.yml
@@ -2,3 +2,7 @@ bone:
toy_id: 1
name: Bone
pet_id: 1
+doll:
+ toy_id: 2
+ name: Doll
+ pet_id: 2

0 comments on commit 7fc3390

Please sign in to comment.
Something went wrong with that request. Please try again.