Forum: Ruby on Rails superclass mismatch for class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5e8fd591aeccbffd6fc93c171ac4fa43?d=identicon&s=25 Aaron Smith (warhero)
on 2007-06-30 20:36
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?
5e8fd591aeccbffd6fc93c171ac4fa43?d=identicon&s=25 Aaron Smith (warhero)
on 2007-06-30 20:39
Aaron Smith 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.
16dffef1e28f8d155f65f3de61b59516?d=identicon&s=25 Kevin Tyll (Guest)
on 2007-07-02 22:39
(Received via mailing list)
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 Smith <rails-mailing-l...@andreas-s.net>
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-07-02 22:51
(Received via mailing list)
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
This topic is locked and can not be replied to.