diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2013-04-24 13:37:33 +0100 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2013-04-24 15:07:39 +0100 |
commit | 1a30cfec2ff6257792dde20105f5986398e2acd3 (patch) | |
tree | 8a3e38523e2573aedcdbacb8e442219b3706c3af /activerecord/lib | |
parent | ed5caf0993ac3504c0216ec65d8800f14daeafa4 (diff) | |
download | rails-1a30cfec2ff6257792dde20105f5986398e2acd3.tar.gz rails-1a30cfec2ff6257792dde20105f5986398e2acd3.tar.bz2 rails-1a30cfec2ff6257792dde20105f5986398e2acd3.zip |
Revert "Revert "`belongs_to :touch` behavior now touches old association when transitioning to new association" until a proper fix is found for #10197"
This reverts commit 7389df139a35436f00876c96d20e81ba23c93f0a.
Conflicts:
activerecord/test/cases/timestamp_test.rb
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/builder/belongs_to.rb | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index c3026dc23c..092230b2f7 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -67,8 +67,19 @@ module ActiveRecord::Associations::Builder def add_touch_callbacks(reflection) mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def belongs_to_touch_after_save_or_destroy_for_#{name} - record = #{name} + foreign_key_field = #{reflection.foreign_key.inspect} + old_foreign_id = attribute_was(foreign_key_field) + + if old_foreign_id + reflection_klass = #{reflection.klass} + old_record = reflection_klass.find_by(reflection_klass.primary_key => old_foreign_id) + + if old_record + old_record.touch #{options[:touch].inspect if options[:touch] != true} + end + end + record = #{name} unless record.nil? || record.new_record? record.touch #{options[:touch].inspect if options[:touch] != true} end |