Skip to content

Commit 034f254

Browse files
Use the correct pk field from the reflected class to find the old record
The implementation was using the source class foreign key field instead of the reflected primary key one to find the old record. For instance, for this scenario class Bulb < ActiveRecord::Base belongs_to :car, :touch => true end class Car < ActiveRecord::Base has_many :bulbs end the current implementation was trying to do this query: Car.where(car_id: X).first where we should be doing this query: Car.where(id: X).first This should hopefully fix the build.
1 parent 797fcdf commit 034f254

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ def belongs_to_touch_after_save_or_destroy_for_#{name}
7171
foreign_key_field = #{reflection.foreign_key.inspect}
7272
if changed_attributes.key?(foreign_key_field)
7373
reflection_klass = #{reflection.klass}
74+
primary_key_field = reflection_klass.primary_key
7475
old_foreign_id = changed_attributes[foreign_key_field]
75-
old_record = reflection_klass.where(foreign_key_field => old_foreign_id).first
76+
old_record = reflection_klass.where(primary_key_field => old_foreign_id).first
7677
if old_record
7778
old_record.touch #{options[:touch].inspect if options[:touch] != true}
7879
end

0 commit comments

Comments
 (0)