Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord::Base.compute_type swallows NoMethodError

#4751

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.

Reported by Andrew Bloomgarden · June 2nd, 2010 @ 06:56 AM

State: resolved
Milestone: 3.0.2
Assigned to: nobody
Importance: Low

Activity

  1. Andrew Bloomgarden
    Andrew Bloomgarden
    • Tag changed from bug, rails3, regression to bug, patch, rails3, regression

    Here's the patch and a (now-passing) test.

    June 2nd, 2010 @ 06:59 AM

  2. Andrew Bloomgarden
    Andrew Bloomgarden

    I've updated the patch to include tests on compute_type's standard behavior.

    June 3rd, 2010 @ 01:37 AM

  3. Michael Koziarski
  4. Andrew White
    Andrew White

    Perhaps 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)
    

    June 7th, 2010 @ 09:33 AM

  5. Repository
    Repository
    • State changed from new to resolved

    (from [0e9b9d59859efa46a82b56e0715784fa52656650]) Fix ActiveRecord::Base.compute_type swallowing NoMethodError. [#4751 ActiveRecord::Base.compute_type swallows NoMethodError state:resolved]

    Signed-off-by: David Heinemeier Hansson david@loudthinking.com
    http://github.com/rails/rails/commit/0e9b9d59859efa46a82b56e0715784...

    June 8th, 2010 @ 08:34 PM

  6. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM