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.
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