From 2a8994122588a2bafb8cf052f2ca1a49a2b758d6 Mon Sep 17 00:00:00 2001
From: Andrew White <andyw@pixeltrix.co.uk>
Date: Wed, 24 Apr 2013 15:03:03 +0100
Subject: 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.
---
 activerecord/lib/active_record/associations/builder/belongs_to.rb | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index 092230b2f7..543a0247d1 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -71,8 +71,8 @@ module ActiveRecord::Associations::Builder
           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}
-- 
cgit v1.2.3