Superclass mismatch for class

Having a slight issue with this “superclass mismatch for class” error.
I’m developing a plugin and having to load controllers dynamically.
Like:

class MyController < ApplicationController
end
load(‘my_controller.rb’)
@klaz = Object.const_get(‘MyController’)

When I do this, It works the FIRST time, be then on subsequent requests
I am getting “superclass mismatch for class MyController”

Any ideas?

Aaron S. wrote:

Having a slight issue with this “superclass mismatch for class” error.
I’m developing a plugin and having to load controllers dynamically.
Like:

class MyController < ApplicationController
end
load(‘my_controller.rb’)
@klaz = Object.const_get(‘MyController’)

When I do this, It works the FIRST time, be then on subsequent requests
I am getting “superclass mismatch for class MyController”

Any ideas?

I just tried one other thing. If the controller class extends
ActionController::Base instead of ApplicationController, it works fine:

This works every time:
class MyController < ActionController::Base
end
load(‘my_controller.rb’)
@klaz = Object.const_get(‘MyController’)

What would be causing the failure when extending from
ApplicationController?

Thanks.

I’m experiencing the exact same issue. After reading your post, I’ve
confirmed that my code also works if inheriting ActionController::Base
and not ApplicationController. I also have subfolders within the
controllers folder, and they all load fine, even when inheriting
ApplicationController. Only the controllers in the controllers folder
produce the superclass mismatch error on subsequent calls.

After a full day of debugging this, I am no closer to finding the
issue. I have however, found a workaround. For some reason calling
Object.const_get(‘MyController’) before load(‘MyController’)
eliminates the error.

It’s a horrible workaround, but I must move on.

If anyone finds the real solution, I’d love to know what it is so I
can get rid of this hack.

On Jun 30, 2:36 pm, Aaron S. [email protected]

You’re trying to redefine a class (through multiple but different
requires
most likely) with different subclasses. Try this out:

file1.rb:
class Parent1
end

class Tester < Parent1
end

file2.rb
class Parent2
end

class Tester < Parent2
end

test.rb
require ‘file1’
require ‘file2’ # => Superclass Mismatch for Tester file2.rb

This error is slightly cryptic but does make sense when you know what it
means. The error is named as such because of the open classes nature of
Ruby. Were the superclasses the same, then it’d be simply adding
functionality to an existing class. However, as the classes are of
different
types, Ruby can’t know what to do with them and thus dies.

So make sure you’re not trying to munge up the Object namespace with
classes
named the same but are different types.

I’m not sure of the exact reason as to why ApplicationController fails
and
ActionController::Base works, but do remember that Rails does a lot of
strange things to the ObjectSpace.

Jason