Permalink
Browse files

Lookup the class at runtime, not when the association is built

Trying to lookup the parent class when the association is being built
runs the risk of generating uninitialized constant errors because
classes haven't been fully defined yet. To work around this we look up
the class at runtime through the `association` method.

Fixes #10197.
  • Loading branch information...
pixeltrix committed Apr 24, 2013
1 parent 07e489c commit 2a8994122588a2bafb8cf052f2ca1a49a2b758d6
Showing with 2 additions and 2 deletions.
  1. +2 −2 activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -71,8 +71,8 @@ def belongs_to_touch_after_save_or_destroy_for_#{name}
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)
+ klass = association(#{name.inspect}).klass
+ old_record = klass.find_by(klass.primary_key => old_foreign_id)
if old_record
old_record.touch #{options[:touch].inspect if options[:touch] != true}

1 comment on commit 2a89941

@carlosantoniodasilva

This comment has been minimized.

Show comment
Hide comment

Nice 👍

Please sign in to comment.