This project is archived and is in readonly mode.

ActiveRecord::Base.compute_type swallows NoMethodError
Reported by Andrew Bloomgarden | June 2nd, 2010 @ 06:56 AM | in 3.0.2
If you have a model that has an invalid method at the class level, like so:
class InvalidModel < ActiveRecord::Base
  not_a_real_method
end
and another model that uses it as an association:
class OtherModel < ActiveRecord::Base
  has_many :invalid_models
end
when InvalidModel is autoloaded via something like
other_model_instance.invalid_models, it should raise a
NoMethodError.
However, the current implementation of compute_type, which Reflection calls, swallows every NameError assuming that means a constantize failure. NoMethodError is a subclass of NameError, though, so you just get an "uninitialized constant OtherModel::InvalidModel" error. Very misleading, and it didn't work like that in Rails 2.x.
I have a patch & test forthcoming.
Comments and changes to this ticket
- 
            
         Andrew Bloomgarden June 2nd, 2010 @ 06:59 AM- Tag changed from bug, rails3, regression to bug, patch, rails3, regression
 Here's the patch and a (now-passing) test. 
- 
            
         Andrew Bloomgarden June 3rd, 2010 @ 01:37 AMI've updated the patch to include tests on compute_type's standard behavior. 
- 
         Michael Koziarski June 4th, 2010 @ 05:15 AM- Milestone cleared.
 
- 
         Andrew White June 7th, 2010 @ 09:33 AMPerhaps we should re-raise the error if it's not explicity NameError since although NoMethodError is the only standard error that's a subclass of NameError, Rails adds AR::UnknownAttributeError and AR::MissingAttributeError plus there are plugins and gems that add subclasses of NameError as well. e.g: rescue NameError => e raise e unless e.instance_of(NameError)
- 
         Repository June 8th, 2010 @ 08:34 PM- State changed from new to resolved
 (from [0e9b9d59859efa46a82b56e0715784fa52656650]) Fix ActiveRecord::Base.compute_type swallowing NoMethodError. [#4751 state:resolved] Signed-off-by: David Heinemeier Hansson david@loudthinking.com 
 http://github.com/rails/rails/commit/0e9b9d59859efa46a82b56e0715784...
- 
         Jeremy Kemper October 15th, 2010 @ 11:01 PM- Milestone set to 3.0.2
- Importance changed from  to Low
 
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 
         4751 
          ActiveRecord::Base.compute_type swallows NoMethodError
        (from [0e9b9d59859efa46a82b56e0715784fa52656650])
Fix Act... 4751 
          ActiveRecord::Base.compute_type swallows NoMethodError
        (from [0e9b9d59859efa46a82b56e0715784fa52656650])
Fix Act...
 Andrew Bloomgarden
      Andrew Bloomgarden
 Andrew White
      Andrew White
 Jeremy Kemper
      Jeremy Kemper
 Michael Koziarski
      Michael Koziarski
 Santiago Pastorino
      Santiago Pastorino