From 8fccbc1ad4fff215c63d17b9321fc69ad17e89dc Mon Sep 17 00:00:00 2001 From: Adam Gamble Date: Thu, 31 Jan 2013 21:54:41 -0600 Subject: Modifies belong_to touch callback to touch old associations also #9091 --- .../lib/active_record/associations/builder/belongs_to.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/lib/active_record/associations/builder/belongs_to.rb') diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 97b1ff18e2..6b479968c8 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -48,6 +48,16 @@ module ActiveRecord::Associations::Builder 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 -- cgit v1.2.3