aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/builder/belongs_to.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2013-04-03 09:56:43 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2013-04-03 09:56:43 -0700
commit9035cfc0dfc2a34b27df926afd6dd1b7862c1076 (patch)
tree0837534502212349cef63fb46a4329e519885002 /activerecord/lib/active_record/associations/builder/belongs_to.rb
parentdb924e1257f37dff441fee7c6e2bbd8ba382d7a6 (diff)
parent8fccbc1ad4fff215c63d17b9321fc69ad17e89dc (diff)
downloadrails-9035cfc0dfc2a34b27df926afd6dd1b7862c1076.tar.gz
rails-9035cfc0dfc2a34b27df926afd6dd1b7862c1076.tar.bz2
rails-9035cfc0dfc2a34b27df926afd6dd1b7862c1076.zip
Merge pull request #9141 from adamgamble/issue-9091
belongs_to :touch should touch old record when transitioning.
Diffstat (limited to 'activerecord/lib/active_record/associations/builder/belongs_to.rb')
-rw-r--r--activerecord/lib/active_record/associations/builder/belongs_to.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index 9ac561b997..579d7789bd 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -68,6 +68,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