Permalink
Browse files

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.
  • Loading branch information...
1 parent 249876b commit 8f84cdd06f85c65c1949ea227285bbdeb47c5b14 @pixeltrix pixeltrix committed Apr 4, 2013
View
4 activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -68,11 +68,11 @@ def add_touch_callbacks(reflection)
def belongs_to_touch_after_save_or_destroy_for_#{name}
record = #{name}
- foreign_key_field = #{reflection.foreign_key}
+ foreign_key_field = #{reflection.foreign_key.inspect}
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
+ old_record = reflection_klass.where(foreign_key_field => old_foreign_id).first
if old_record
old_record.touch #{options[:touch].inspect if options[:touch] != true}
end
View
6 activerecord/test/cases/timestamp_test.rb
@@ -192,6 +192,12 @@ def self.name; 'Toy'; end
old_pet.update_columns(updated_at: time)
new_pet.update_columns(updated_at: time)
+ old_pet.reload
+ new_pet.reload
+
+ assert_equal time, new_pet.updated_at
+ assert_equal time, old_pet.updated_at
+
toy1.pet = new_pet
toy1.save!

4 comments on commit 8f84cdd

@carlosantoniodasilva
Ruby on Rails member

❤️

@carlosantoniodasilva
Ruby on Rails member

Ops, that seems to have failed the build =(

@carlosantoniodasilva
Ruby on Rails member

I hope 034f254 will fix it :)

@carlosantoniodasilva
Ruby on Rails member

@pixeltrix looks like it didn't =(, although it is passing for me locally. Timezones are hard 😄, we can talk about it tomorrow.

Please sign in to comment.