Skip to content

Commit 8f84cdd

Browse files
committed
Use inspect when writing the foreign key from the reflection
If we don't use inspect inside the class_eval block then the foreign key is written without quotes causing us to fetch the foreign key value and not the column name.
1 parent 249876b commit 8f84cdd

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

activerecord/lib/active_record/associations/builder/belongs_to.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ def add_touch_callbacks(reflection)
6868
def belongs_to_touch_after_save_or_destroy_for_#{name}
6969
record = #{name}
7070
71-
foreign_key_field = #{reflection.foreign_key}
71+
foreign_key_field = #{reflection.foreign_key.inspect}
7272
if changed_attributes.key?(foreign_key_field)
7373
reflection_klass = #{reflection.klass}
7474
old_foreign_id = changed_attributes[foreign_key_field]
75-
old_record = reflection_klass.where(foreign_key_field.to_sym => old_foreign_id).first
75+
old_record = reflection_klass.where(foreign_key_field => old_foreign_id).first
7676
if old_record
7777
old_record.touch #{options[:touch].inspect if options[:touch] != true}
7878
end

activerecord/test/cases/timestamp_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ def self.name; 'Toy'; end
192192
old_pet.update_columns(updated_at: time)
193193
new_pet.update_columns(updated_at: time)
194194

195+
old_pet.reload
196+
new_pet.reload
197+
198+
assert_equal time, new_pet.updated_at
199+
assert_equal time, old_pet.updated_at
200+
195201
toy1.pet = new_pet
196202
toy1.save!
197203

0 commit comments

Comments
 (0)