Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6348 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

Sessions cannot autoload namespaced classes

Reported by: ulysses Assigned to: ulysses
Priority: normal Milestone: 1.2
Component: ActiveRecord Version: edge
Severity: normal Keywords: dependencies
Cc: jaylev

Description

OK, the problem seems to be on line 225 of dependencies.rb; you can't call Object::const_defined?("SomeModule::SomeClass"), or you get a NameError, which silently disappears somehow. I'm not qualified to recommend a fix, let alone patch it.

New ticket, or stay here?

Change History

10/04/06 21:04:29 changed by ulysses

References #4638

10/04/06 21:08:45 changed by ulysses

If we look at line 225, we see:

from_mod.const_defined?(const_name)

The dependencies code doesn't expect const_name to be a qualified constant; we can either change that expectation (which I'm not sure is right) or change the code that is providing the qualified const_name.

10/04/06 21:11:08 changed by ulysses

Possible fix:

Index: actionpack/lib/action_controller/cgi_process.rb
===================================================================
--- actionpack/lib/action_controller/cgi_process.rb	(revision 5226)
+++ actionpack/lib/action_controller/cgi_process.rb	(working copy)
@@ -143,7 +143,7 @@
       rescue ArgumentError => argument_error
         if argument_error.message =~ %r{undefined class/module ([\w:]+)}
           begin
-            Module.const_missing($1)
+            $1.constantize
           rescue LoadError, NameError => const_error
             raise ActionController::SessionRestoreError, <<-end_msg
 Session contains objects whose class definition isn\'t available.

10/09/06 02:51:24 changed by ulysses

With

class Foo::Mod
  attr_accessor :message
end

If we Marshal.dump an instance:

>> Marshal.dump m
=> "\004\010o:\rFoo::Mod\006:\r@message\"\ahi"

And then try to load it in a new console:

>> Marshal.load "\004\010o:\rFoo::Mod\006:\r@message\"\ahi"
ArgumentError: undefined class/module Foo::

This error isn't very useful, and lacking a way to extract the qualified class name from the marshalled string this proves to be a stopper.

01/28/07 06:17:00 changed by ulysses

  • summary changed from const_defined? called with qualified constant path to Sessions cannot autoload namespaced classes.

01/28/07 06:19:09 changed by ulysses

  • status changed from new to closed.
  • resolution set to fixed.

(In [6054]) Change session restoration to allow namespaced models to be autoloaded. Closes #6348.