Skip to content
Browse files

Merge pull request #9141 from adamgamble/issue-9091

belongs_to :touch should touch old record when transitioning.
  • Loading branch information...
2 parents db924e1 + 8fccbc1 commit 9035cfc0dfc2a34b27df926afd6dd1b7862c1076 @dhh dhh committed Apr 3, 2013
View
22 activerecord/CHANGELOG.md
@@ -598,6 +598,28 @@
*Aaron Stone + Rafael Mendonça França*
* `Relation#merge` now only overwrites where values on the LHS of the
+=======
+* Belongs_to :touch behavior now touches old association when
+ transitioning to new association
+
+ class Passenger < ActiveRecord::Base
+ belongs_to :car, touch: true
+ end
+
+ car_1 = Car.create
+ car_2 = Car.create
+
+ passenger = Passenger.create :car => car_1
+
+ passenger.car = car_2
+ passenger.save
+
+ Previously only car_2 would be touched. Now both car_1 and car_2
+ will be touched.
+
+ *Adam Gamble*
+
+* Relation#merge now only overwrites where values on the LHS of the
@PikachuEXE
PikachuEXE added a note Apr 3, 2013

Seems a duplicated line with L600

@carlosantoniodasilva
Ruby on Rails member

Thanks, it's been fixed in 1e703f5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
merge. Consider:
left = Person.where(age: [13, 14, 15])
View
10 activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -68,6 +68,16 @@ def add_touch_callbacks(reflection)
def belongs_to_touch_after_save_or_destroy_for_#{name}
record = #{name}
+ foreign_key_field = #{reflection.foreign_key}
+ if changed_attributes.key?(foreign_key_field)
+ reflection_klass = #{reflection.klass}
+ old_foreign_id = changed_attributes[foreign_key_field]
+ old_record = reflection_klass.where(foreign_key_field.to_sym => old_foreign_id).first
+ if old_record
+ old_record.touch #{options[:touch].inspect if options[:touch] != true}
+ end
+ end
+
unless record.nil? || record.new_record?
record.touch #{options[:touch].inspect if options[:touch] != true}
end
View
26 activerecord/test/cases/timestamp_test.rb
@@ -176,6 +176,32 @@ 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
+ 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
+
+ toy2 = klass.find(2)
+ new_pet = toy2.pet
+ time = 3.days.ago
+
+ old_pet.update_columns(updated_at: time)
+ new_pet.update_columns(updated_at: time)
+
+ toy1.pet = new_pet
+ toy1.save!
+
+ old_pet.reload
+ new_pet.reload
+
+ assert_not_equal time, new_pet.updated_at
+ assert_not_equal time, old_pet.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 9035cfc

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